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

Commit 1bdc00b1 authored by Sudheer Shanka's avatar Sudheer Shanka Committed by Android (Google) Code Review
Browse files

Merge "Use ApplicationInfo to check if changeId is disabled." into main

parents 046cb571 8929f131
Loading
Loading
Loading
Loading
+8 −1
Original line number Original line Diff line number Diff line
@@ -16,6 +16,7 @@


package com.android.server.am;
package com.android.server.am;


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


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


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

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


@@ -865,25 +865,35 @@ final class BroadcastRecord extends Binder {
    @VisibleForTesting
    @VisibleForTesting
    static @NonNull boolean[] calculateChangeStateForReceivers(@NonNull List<Object> receivers,
    static @NonNull boolean[] calculateChangeStateForReceivers(@NonNull List<Object> receivers,
            long changeId, PlatformCompat platformCompat) {
            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 int count = receivers.size();
        final boolean[] changeStateForReceivers = new boolean[count];
        final boolean[] changeStateForReceivers = new boolean[count];
        for (int i = 0; i < count; ++i) {
        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 boolean isChangeEnabled;
            final int idx = changeStateForUids.indexOfKey(receiverUid);
            final int idx = changeStates.indexOfKey(receiverAppInfo.packageName);
            if (idx >= 0) {
            if (idx >= 0) {
                isChangeEnabled = changeStateForUids.valueAt(idx);
                isChangeEnabled = changeStates.valueAt(idx);
            } else {
            } else {
                isChangeEnabled = platformCompat.isChangeEnabledByUidInternalNoLogging(
                isChangeEnabled = platformCompat.isChangeEnabledInternalNoLogging(
                        changeId, receiverUid);
                        changeId, receiverAppInfo);
                changeStateForUids.put(receiverUid, isChangeEnabled);
                changeStates.put(receiverAppInfo.packageName, isChangeEnabled);
            }
            }
            changeStateForReceivers[i] = isChangeEnabled;
            changeStateForReceivers[i] = isChangeEnabled;
        }
        }
        return changeStateForReceivers;
        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) {
    static int getReceiverUid(@NonNull Object receiver) {
        if (receiver instanceof BroadcastFilter) {
        if (receiver instanceof BroadcastFilter) {
            return ((BroadcastFilter) receiver).owningUid;
            return ((BroadcastFilter) receiver).owningUid;
+7 −2
Original line number Original line Diff line number Diff line
@@ -58,6 +58,7 @@ import com.android.server.compat.PlatformCompat;
import com.android.server.wm.ActivityTaskManagerService;
import com.android.server.wm.ActivityTaskManagerService;


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


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


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


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

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


    private static BroadcastFilter makeMockRegisteredReceiver() {
    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) {
    private BroadcastRecord makeBroadcastRecord(Intent intent) {
@@ -716,9 +720,9 @@ public final class BroadcastQueueModernImplTest extends BaseBroadcastQueueTest {
    @EnableFlags(Flags.FLAG_LIMIT_PRIORITY_SCOPE)
    @EnableFlags(Flags.FLAG_LIMIT_PRIORITY_SCOPE)
    @Test
    @Test
    public void testRunnableAt_Cached_Prioritized_NonDeferrable_changeIdDisabled() {
    public void testRunnableAt_Cached_Prioritized_NonDeferrable_changeIdDisabled() {
        doReturn(false).when(mPlatformCompat).isChangeEnabledByUidInternalNoLogging(
        doReturn(false).when(mPlatformCompat).isChangeEnabledInternalNoLogging(
                eq(BroadcastRecord.LIMIT_PRIORITY_SCOPE),
                eq(BroadcastRecord.LIMIT_PRIORITY_SCOPE),
                eq(getUidForPackage(PACKAGE_GREEN)));
                argThat(appInfoEquals(getUidForPackage(PACKAGE_GREEN))));
        final List receivers = List.of(
        final List receivers = List.of(
                withPriority(makeManifestReceiver(PACKAGE_RED, PACKAGE_RED), 10),
                withPriority(makeManifestReceiver(PACKAGE_RED, PACKAGE_RED), 10),
                withPriority(makeManifestReceiver(PACKAGE_GREEN, PACKAGE_GREEN), -10));
                withPriority(makeManifestReceiver(PACKAGE_GREEN, PACKAGE_GREEN), -10));
@@ -1288,9 +1292,9 @@ public final class BroadcastQueueModernImplTest extends BaseBroadcastQueueTest {
    @SuppressWarnings("GuardedBy")
    @SuppressWarnings("GuardedBy")
    @Test
    @Test
    public void testDeliveryGroupPolicy_prioritized_diffReceivers_changeIdDisabled() {
    public void testDeliveryGroupPolicy_prioritized_diffReceivers_changeIdDisabled() {
        doReturn(false).when(mPlatformCompat).isChangeEnabledByUidInternalNoLogging(
        doReturn(false).when(mPlatformCompat).isChangeEnabledInternalNoLogging(
                eq(BroadcastRecord.LIMIT_PRIORITY_SCOPE),
                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 screenOn = new Intent(Intent.ACTION_SCREEN_ON);
        final Intent screenOff = new Intent(Intent.ACTION_SCREEN_OFF);
        final Intent screenOff = new Intent(Intent.ACTION_SCREEN_OFF);
@@ -1823,9 +1827,9 @@ public final class BroadcastQueueModernImplTest extends BaseBroadcastQueueTest {
    @SuppressWarnings("GuardedBy")
    @SuppressWarnings("GuardedBy")
    @Test
    @Test
    public void testDeliveryDeferredForCached_changeIdDisabled() throws Exception {
    public void testDeliveryDeferredForCached_changeIdDisabled() throws Exception {
        doReturn(false).when(mPlatformCompat).isChangeEnabledByUidInternalNoLogging(
        doReturn(false).when(mPlatformCompat).isChangeEnabledInternalNoLogging(
                eq(BroadcastRecord.LIMIT_PRIORITY_SCOPE),
                eq(BroadcastRecord.LIMIT_PRIORITY_SCOPE),
                eq(getUidForPackage(PACKAGE_GREEN)));
                argThat(appInfoEquals(getUidForPackage(PACKAGE_GREEN))));


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


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


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


        // Enqueue a normal broadcast that will go to several processes, and
        // Enqueue a normal broadcast that will go to several processes, and
        // then enqueue a foreground broadcast that risks reordering
        // 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 receiverBlueApp = makeActiveProcessRecord(PACKAGE_BLUE);
        final ProcessRecord receiverGreenApp = makeActiveProcessRecord(PACKAGE_GREEN);
        final ProcessRecord receiverGreenApp = makeActiveProcessRecord(PACKAGE_GREEN);


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


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