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

Commit 7514e1b8 authored by junyulai's avatar junyulai
Browse files

[FUI06] Stop using NetworkInfo in NetworkState from external callers

This is achieved by:
  1. Use legacy network type inside NetworkState to replace the
     needs of referencing NetworkInfo.getType().
  2. Let getAllNetworkState only return networks with isConnected()
     equals true. This allows callers such as NPMS or NSS does not
     have to reference to NetworkInfo.isConnected().

Test: atest FrameworksNetTests NetworkPolicyManagerServiceTest
Bug: 174123988
Change-Id: I1c4eb08d18ca973eb8f41d06258872eabc0006b8
parent f6c7f61d
Loading
Loading
Loading
Loading
+4 −4
Original line number Diff line number Diff line
@@ -31,7 +31,7 @@ import java.util.Objects;

/**
 * Network definition that includes strong identity. Analogous to combining
 * {@link NetworkInfo} and an IMSI.
 * {@link NetworkCapabilities} and an IMSI.
 *
 * @hide
 */
@@ -159,7 +159,7 @@ public class NetworkIdentity implements Comparable<NetworkIdentity> {
     */
    public static NetworkIdentity buildNetworkIdentity(Context context, NetworkState state,
            boolean defaultNetwork, @NetworkType int subType) {
        final int type = state.networkInfo.getType();
        final int legacyType = state.legacyNetworkType;

        String subscriberId = null;
        String networkId = null;
@@ -170,7 +170,7 @@ public class NetworkIdentity implements Comparable<NetworkIdentity> {

        subscriberId = state.subscriberId;

        if (type == TYPE_WIFI) {
        if (legacyType == TYPE_WIFI) {
            if (state.networkCapabilities.getSsid() != null) {
                networkId = state.networkCapabilities.getSsid();
                if (networkId == null) {
@@ -183,7 +183,7 @@ public class NetworkIdentity implements Comparable<NetworkIdentity> {
            }
        }

        return new NetworkIdentity(type, subType, subscriberId, networkId, roaming, metered,
        return new NetworkIdentity(legacyType, subType, subscriberId, networkId, roaming, metered,
                defaultNetwork);
    }

+6 −0
Original line number Diff line number Diff line
@@ -41,6 +41,7 @@ public class NetworkState implements Parcelable {
    public final Network network;
    public final String subscriberId;
    public final String networkId;
    public final int legacyNetworkType;

    private NetworkState() {
        networkInfo = null;
@@ -49,6 +50,7 @@ public class NetworkState implements Parcelable {
        network = null;
        subscriberId = null;
        networkId = null;
        legacyNetworkType = 0;
    }

    public NetworkState(@NonNull NetworkInfo networkInfo, @NonNull LinkProperties linkProperties,
@@ -60,6 +62,8 @@ public class NetworkState implements Parcelable {
        this.network = network;
        this.subscriberId = subscriberId;
        this.networkId = networkId;
        // TODO: Pass legacyNetworkType directly from parameters and remove NetworkInfo.
        this.legacyNetworkType = this.networkInfo.getType();

        // This object is an atomic view of a network, so the various components
        // should always agree on roaming state.
@@ -80,6 +84,7 @@ public class NetworkState implements Parcelable {
        network = in.readParcelable(null);
        subscriberId = in.readString();
        networkId = in.readString();
        legacyNetworkType = in.readInt();
    }

    @Override
@@ -95,6 +100,7 @@ public class NetworkState implements Parcelable {
        out.writeParcelable(network, flags);
        out.writeString(subscriberId);
        out.writeString(networkId);
        out.writeInt(legacyNetworkType);
    }

    @UnsupportedAppUsage
+2 −1
Original line number Diff line number Diff line
@@ -1854,7 +1854,8 @@ public class ConnectivityService extends IConnectivityManager.Stub
        final ArrayList<NetworkState> result = new ArrayList<>();
        for (Network network : getAllNetworks()) {
            final NetworkAgentInfo nai = getNetworkAgentInfoForNetwork(network);
            if (nai != null) {
            // TODO: Consider include SUSPENDED networks.
            if (nai != null && nai.networkInfo.isConnected()) {
                // TODO (b/73321673) : NetworkState contains a copy of the
                // NetworkCapabilities, which may contain UIDs of apps to which the
                // network applies. Should the UIDs be cleared so as not to leak or
+8 −10
Original line number Diff line number Diff line
@@ -1960,7 +1960,7 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
            if (state.network != null) {
                mNetIdToSubId.put(state.network.netId, parseSubId(state));
            }
            if (state.networkInfo != null && state.networkInfo.isConnected()) {

            // Policies matched by NPMS only match by subscriber ID or by ssid. Thus subtype
            // in the object created here is never used and its value doesn't matter, so use
            // NETWORK_TYPE_UNKNOWN.
@@ -1968,7 +1968,6 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
                    true, TelephonyManager.NETWORK_TYPE_UNKNOWN /* subType */);
            identified.put(state, ident);
        }
        }

        final ArraySet<String> newMeteredIfaces = new ArraySet<>();
        long lowestRule = Long.MAX_VALUE;
@@ -2042,8 +2041,7 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
        // One final pass to catch any metered ifaces that don't have explicitly
        // defined policies; typically Wi-Fi networks.
        for (NetworkState state : states) {
            if (state.networkInfo != null && state.networkInfo.isConnected()
                    && !state.networkCapabilities.hasCapability(NET_CAPABILITY_NOT_METERED)) {
            if (!state.networkCapabilities.hasCapability(NET_CAPABILITY_NOT_METERED)) {
                matchingIfaces.clear();
                collectIfaces(matchingIfaces, state);
                for (int j = matchingIfaces.size() - 1; j >= 0; j--) {
+73 −76
Original line number Diff line number Diff line
@@ -96,7 +96,6 @@ import android.net.LinkProperties;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.NetworkIdentity;
import android.net.NetworkInfo;
import android.net.NetworkStack;
import android.net.NetworkState;
import android.net.NetworkStats;
@@ -1264,7 +1263,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub {

    /**
     * Inspect all current {@link NetworkState} to derive mapping from {@code iface} to {@link
     * NetworkStatsHistory}. When multiple {@link NetworkInfo} are active on a single {@code iface},
     * NetworkStatsHistory}. When multiple networks are active on a single {@code iface},
     * they are combined under a single {@link NetworkIdentitySet}.
     */
    @GuardedBy("mStatsLock")
@@ -1294,8 +1293,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
        final boolean combineSubtypeEnabled = mSettings.getCombineSubtypeEnabled();
        final ArraySet<String> mobileIfaces = new ArraySet<>();
        for (NetworkState state : states) {
            if (state.networkInfo.isConnected()) {
                final boolean isMobile = isNetworkTypeMobile(state.networkInfo.getType());
            final boolean isMobile = isNetworkTypeMobile(state.legacyNetworkType);
            final boolean isDefault = ArrayUtils.contains(mDefaultNetworks, state.network);
            final int subType = combineSubtypeEnabled ? SUBTYPE_COMBINED
                    : getSubTypeForState(state);
@@ -1374,7 +1372,6 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
                }
            }
        }
        }

        mMobileIfaces = mobileIfaces.toArray(new String[mobileIfaces.size()]);
    }
Loading