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

Commit 28b62d47 authored by Michael Groover's avatar Michael Groover
Browse files

Add required flag to registerReceiver call in NetworkMonitor

Android T adds support to allow a runtime receiver to be registered as
not exported, but to ensure apps can take advantage of this, calls to
registerReceiver must specify a flag indicating whether the receiver
should be exported for apps targeting T+ that are registering for
non-system broadcasts. This commit adds the RECEIVER_NOT_EXPORTED
flag to the call to registerReceiver in NetworkMonitor since this
action is only broadcast via a PendingIntent created by the system.

Note, this change will only affect devices running T; prior to T,
there was no way to mark a dynamic receiver as unexported, so this
change uses the platform's default of 0 as the value for the flags
passed to Context#registerReceiver on devices running Q through S.
There's also basic testing in ACTS under tools/test/connectivity/
acts_tests/tests/google/net/CaptivePortalTest.py; while this is not
a blocking test, it will report if there are any issues with this
flag in T.

Bug: 161145287
Fixes: 216213421
Test: atest NetworkMonitorTest
Change-Id: Id8d623fde0715529afa68b3837e81fbdb1a66d12
parent 2fa6fd33
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -34,4 +34,11 @@ public class ConstantsShim extends com.android.networkstack.apishim.api30.Consta
    // When removing this shim, the version in NetworkMonitorUtils should be removed too.
    // TODO: add TRANSPORT_TEST to system API in API 31 (it is only a test API as of R)
    public static final int TRANSPORT_TEST = 7;

    /**
     * Flag for {@link android.content.Context#registerReceiver}: The receiver cannot receive
     * broadcasts from other apps; has the same behavior as marking a statically registered receiver
     * with "exported=false".
     */
    public static final int RECEIVER_NOT_EXPORTED = 0x4;
}
+4 −1
Original line number Diff line number Diff line
@@ -83,6 +83,7 @@ import static com.android.net.module.util.ConnectivityUtils.isIPv6ULA;
import static com.android.net.module.util.DeviceConfigUtils.getResBooleanConfig;
import static com.android.networkstack.apishim.ConstantsShim.DETECTION_METHOD_DNS_EVENTS;
import static com.android.networkstack.apishim.ConstantsShim.DETECTION_METHOD_TCP_METRICS;
import static com.android.networkstack.apishim.ConstantsShim.RECEIVER_NOT_EXPORTED;
import static com.android.networkstack.apishim.ConstantsShim.TRANSPORT_TEST;
import static com.android.networkstack.util.DnsUtils.PRIVATE_DNS_PROBE_HOST_SUFFIX;
import static com.android.networkstack.util.DnsUtils.TYPE_ADDRCONFIG;
@@ -157,6 +158,7 @@ import com.android.internal.annotations.GuardedBy;
import com.android.internal.util.RingBufferIndices;
import com.android.internal.util.State;
import com.android.internal.util.StateMachine;
import com.android.modules.utils.build.SdkLevel;
import com.android.net.module.util.DeviceConfigUtils;
import com.android.net.module.util.NetworkStackConstants;
import com.android.networkstack.NetworkStackNotifier;
@@ -1428,7 +1430,8 @@ public class NetworkMonitor extends StateMachine {
            mToken = token;
            mWhat = what;
            mAction = action + "_" + mCleartextDnsNetwork.getNetworkHandle() + "_" + token;
            mContext.registerReceiver(this, new IntentFilter(mAction));
            final int flags = SdkLevel.isAtLeastT() ? RECEIVER_NOT_EXPORTED : 0;
            mContext.registerReceiver(this, new IntentFilter(mAction), flags);
        }
        public PendingIntent getPendingIntent() {
            final Intent intent = new Intent(mAction);
+5 −0
Original line number Diff line number Diff line
@@ -555,6 +555,11 @@ public class NetworkMonitorTest {
            mRegisteredReceivers.add(invocation.getArgument(0));
            return new Intent();
        });
        when(mContext.registerReceiver(any(BroadcastReceiver.class), any(), anyInt())).then(
                (invocation) -> {
                    mRegisteredReceivers.add(invocation.getArgument(0));
                    return new Intent();
                });

        doAnswer((invocation) -> {
            mRegisteredReceivers.remove(invocation.getArgument(0));