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

Commit 8929f131 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: 371307720
Test: atest services/tests/mockingservicestests/src/com/android/server/am/BroadcastRecordTest.java
Test: atest services/tests/mockingservicestests/src/com/android/server/am/BroadcastQueueTest.java
Test: atest services/tests/mockingservicestests/src/com/android/server/am/BroadcastQueueModernImplTest.java
Flag: com.android.server.am.limit_priority_scope
Change-Id: Icacea195d7cb2824c52df125adf598b7ae4782c9
parent 90cd3b25
Loading
Loading
Loading
Loading
+8 −1
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package com.android.server.am;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.compat.annotation.ChangeId;
import android.compat.annotation.EnabledSince;
@@ -56,11 +57,12 @@ public final class BroadcastFilter extends IntentFilter {
    final boolean visibleToInstantApp;
    public final boolean exported;
    final int initialPriority;
    final ApplicationInfo applicationInfo;

    BroadcastFilter(IntentFilter _filter, ReceiverList _receiverList,
            String _packageName, String _featureId, String _receiverId, String _requiredPermission,
            int _owningUid, int _userId, boolean _instantApp, boolean _visibleToInstantApp,
            boolean _exported, ApplicationInfo applicationInfo, PlatformCompat platformCompat) {
            boolean _exported, ApplicationInfo _applicationInfo, PlatformCompat platformCompat) {
        super(_filter);
        receiverList = _receiverList;
        packageName = _packageName;
@@ -72,6 +74,7 @@ public final class BroadcastFilter extends IntentFilter {
        instantApp = _instantApp;
        visibleToInstantApp = _visibleToInstantApp;
        exported = _exported;
        applicationInfo = _applicationInfo;
        initialPriority = getPriority();
        setPriority(calculateAdjustedPriority(owningUid, initialPriority,
                applicationInfo, platformCompat));
@@ -87,6 +90,10 @@ public final class BroadcastFilter extends IntentFilter {
        return null;
    }

    public @NonNull ApplicationInfo getApplicationInfo() {
        return applicationInfo;
    }

    @NeverCompile
    public void dumpDebug(ProtoOutputStream proto, long fieldId) {
        long token = proto.start(fieldId);
+18 −8
Original line number Diff line number Diff line
@@ -49,6 +49,7 @@ import android.content.IIntentReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.ResolveInfo;
import android.os.Binder;
import android.os.Bundle;
@@ -57,7 +58,6 @@ import android.os.UserHandle;
import android.util.ArrayMap;
import android.util.IntArray;
import android.util.PrintWriterPrinter;
import android.util.SparseBooleanArray;
import android.util.TimeUtils;
import android.util.proto.ProtoOutputStream;

@@ -865,25 +865,35 @@ final class BroadcastRecord extends Binder {
    @VisibleForTesting
    static @NonNull boolean[] calculateChangeStateForReceivers(@NonNull List<Object> receivers,
            long changeId, PlatformCompat platformCompat) {
        final SparseBooleanArray changeStateForUids = new SparseBooleanArray();
        // TODO: b/371307720 - Remove this method as we are already avoiding the packagemanager
        // calls by checking the changeId state using ApplicationInfos.
        final ArrayMap<String, Boolean> changeStates = new ArrayMap<>();
        final int count = receivers.size();
        final boolean[] changeStateForReceivers = new boolean[count];
        for (int i = 0; i < count; ++i) {
            final int receiverUid = getReceiverUid(receivers.get(i));
            final ApplicationInfo receiverAppInfo = getReceiverAppInfo(receivers.get(i));
            final boolean isChangeEnabled;
            final int idx = changeStateForUids.indexOfKey(receiverUid);
            final int idx = changeStates.indexOfKey(receiverAppInfo.packageName);
            if (idx >= 0) {
                isChangeEnabled = changeStateForUids.valueAt(idx);
                isChangeEnabled = changeStates.valueAt(idx);
            } else {
                isChangeEnabled = platformCompat.isChangeEnabledByUidInternalNoLogging(
                        changeId, receiverUid);
                changeStateForUids.put(receiverUid, isChangeEnabled);
                isChangeEnabled = platformCompat.isChangeEnabledInternalNoLogging(
                        changeId, receiverAppInfo);
                changeStates.put(receiverAppInfo.packageName, isChangeEnabled);
            }
            changeStateForReceivers[i] = isChangeEnabled;
        }
        return changeStateForReceivers;
    }

    static ApplicationInfo getReceiverAppInfo(@NonNull Object receiver) {
        if (receiver instanceof BroadcastFilter) {
            return ((BroadcastFilter) receiver).getApplicationInfo();
        } else {
            return ((ResolveInfo) receiver).activityInfo.applicationInfo;
        }
    }

    static int getReceiverUid(@NonNull Object receiver) {
        if (receiver instanceof BroadcastFilter) {
            return ((BroadcastFilter) receiver).owningUid;
+7 −2
Original line number Diff line number Diff line
@@ -58,6 +58,7 @@ import com.android.server.compat.PlatformCompat;
import com.android.server.wm.ActivityTaskManagerService;

import org.junit.Rule;
import org.mockito.ArgumentMatcher;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

@@ -187,8 +188,8 @@ public abstract class BaseBroadcastQueueTest {

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

    public void tearDown() throws Exception {
@@ -308,4 +309,8 @@ public abstract class BaseBroadcastQueueTest {
        app.mOptRecord.setPendingFreeze(pendingFreeze);
        app.mOptRecord.setFrozen(frozen);
    }

    ArgumentMatcher<ApplicationInfo> appInfoEquals(int uid) {
        return test -> (test.uid == uid);
    }
}
+13 −9
Original line number Diff line number Diff line
@@ -47,6 +47,7 @@ import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.nullable;
import static org.mockito.Mockito.doAnswer;
@@ -184,7 +185,10 @@ public final class BroadcastQueueModernImplTest extends BaseBroadcastQueueTest {
    }

    private static BroadcastFilter makeMockRegisteredReceiver() {
        return mock(BroadcastFilter.class);
        final BroadcastFilter filter = mock(BroadcastFilter.class);
        final ApplicationInfo info = makeApplicationInfo(PACKAGE_ORANGE);
        doReturn(info).when(filter).getApplicationInfo();
        return filter;
    }

    private BroadcastRecord makeBroadcastRecord(Intent intent) {
@@ -716,9 +720,9 @@ public final class BroadcastQueueModernImplTest extends BaseBroadcastQueueTest {
    @EnableFlags(Flags.FLAG_LIMIT_PRIORITY_SCOPE)
    @Test
    public void testRunnableAt_Cached_Prioritized_NonDeferrable_changeIdDisabled() {
        doReturn(false).when(mPlatformCompat).isChangeEnabledByUidInternalNoLogging(
        doReturn(false).when(mPlatformCompat).isChangeEnabledInternalNoLogging(
                eq(BroadcastRecord.LIMIT_PRIORITY_SCOPE),
                eq(getUidForPackage(PACKAGE_GREEN)));
                argThat(appInfoEquals(getUidForPackage(PACKAGE_GREEN))));
        final List receivers = List.of(
                withPriority(makeManifestReceiver(PACKAGE_RED, PACKAGE_RED), 10),
                withPriority(makeManifestReceiver(PACKAGE_GREEN, PACKAGE_GREEN), -10));
@@ -1288,9 +1292,9 @@ public final class BroadcastQueueModernImplTest extends BaseBroadcastQueueTest {
    @SuppressWarnings("GuardedBy")
    @Test
    public void testDeliveryGroupPolicy_prioritized_diffReceivers_changeIdDisabled() {
        doReturn(false).when(mPlatformCompat).isChangeEnabledByUidInternalNoLogging(
        doReturn(false).when(mPlatformCompat).isChangeEnabledInternalNoLogging(
                eq(BroadcastRecord.LIMIT_PRIORITY_SCOPE),
                eq(getUidForPackage(PACKAGE_GREEN)));
                argThat(appInfoEquals(getUidForPackage(PACKAGE_GREEN))));

        final Intent screenOn = new Intent(Intent.ACTION_SCREEN_ON);
        final Intent screenOff = new Intent(Intent.ACTION_SCREEN_OFF);
@@ -1823,9 +1827,9 @@ public final class BroadcastQueueModernImplTest extends BaseBroadcastQueueTest {
    @SuppressWarnings("GuardedBy")
    @Test
    public void testDeliveryDeferredForCached_changeIdDisabled() throws Exception {
        doReturn(false).when(mPlatformCompat).isChangeEnabledByUidInternalNoLogging(
        doReturn(false).when(mPlatformCompat).isChangeEnabledInternalNoLogging(
                eq(BroadcastRecord.LIMIT_PRIORITY_SCOPE),
                eq(getUidForPackage(PACKAGE_GREEN)));
                argThat(appInfoEquals(getUidForPackage(PACKAGE_GREEN))));

        final ProcessRecord greenProcess = makeProcessRecord(makeApplicationInfo(PACKAGE_GREEN));
        final ProcessRecord redProcess = makeProcessRecord(makeApplicationInfo(PACKAGE_RED));
@@ -2027,9 +2031,9 @@ public final class BroadcastQueueModernImplTest extends BaseBroadcastQueueTest {
    @Test
    public void testDeliveryDeferredForCached_withInfiniteDeferred_changeIdDisabled()
            throws Exception {
        doReturn(false).when(mPlatformCompat).isChangeEnabledByUidInternalNoLogging(
        doReturn(false).when(mPlatformCompat).isChangeEnabledInternalNoLogging(
                eq(BroadcastRecord.LIMIT_PRIORITY_SCOPE),
                eq(getUidForPackage(PACKAGE_GREEN)));
                argThat(appInfoEquals(getUidForPackage(PACKAGE_GREEN))));
        final ProcessRecord greenProcess = makeProcessRecord(makeApplicationInfo(PACKAGE_GREEN));
        final ProcessRecord redProcess = makeProcessRecord(makeApplicationInfo(PACKAGE_RED));

+6 −4
Original line number Diff line number Diff line
@@ -1659,8 +1659,9 @@ public class BroadcastQueueTest extends BaseBroadcastQueueTest {
        final ProcessRecord receiverGreenApp = makeActiveProcessRecord(PACKAGE_GREEN);
        final ProcessRecord receiverYellowApp = makeActiveProcessRecord(PACKAGE_YELLOW);

        doReturn(false).when(mPlatformCompat).isChangeEnabledByUidInternalNoLogging(
                eq(BroadcastRecord.LIMIT_PRIORITY_SCOPE), eq(receiverBlueApp.uid));
        doReturn(false).when(mPlatformCompat).isChangeEnabledInternalNoLogging(
                eq(BroadcastRecord.LIMIT_PRIORITY_SCOPE),
                argThat(appInfoEquals(receiverBlueApp.uid)));

        // Enqueue a normal broadcast that will go to several processes, and
        // then enqueue a foreground broadcast that risks reordering
@@ -2471,8 +2472,9 @@ public class BroadcastQueueTest extends BaseBroadcastQueueTest {
        final ProcessRecord receiverBlueApp = makeActiveProcessRecord(PACKAGE_BLUE);
        final ProcessRecord receiverGreenApp = makeActiveProcessRecord(PACKAGE_GREEN);

        doReturn(false).when(mPlatformCompat).isChangeEnabledByUidInternalNoLogging(
                eq(BroadcastRecord.LIMIT_PRIORITY_SCOPE), eq(receiverBlueApp.uid));
        doReturn(false).when(mPlatformCompat).isChangeEnabledInternalNoLogging(
                eq(BroadcastRecord.LIMIT_PRIORITY_SCOPE),
                argThat(appInfoEquals(receiverBlueApp.uid)));

        mUidObserver.onUidStateChanged(receiverGreenApp.info.uid,
                ActivityManager.PROCESS_STATE_TOP, 0, ActivityManager.PROCESS_CAPABILITY_NONE);
Loading