[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
This commit is contained in:
junyulai
2020-12-29 19:17:01 +08:00
parent ab4b0cdccc
commit d49aab92c9
3 changed files with 8 additions and 2 deletions

View File

@@ -41,6 +41,7 @@ public class NetworkState implements Parcelable {
public final Network network; public final Network network;
public final String subscriberId; public final String subscriberId;
public final String networkId; public final String networkId;
public final int legacyNetworkType;
private NetworkState() { private NetworkState() {
networkInfo = null; networkInfo = null;
@@ -49,6 +50,7 @@ public class NetworkState implements Parcelable {
network = null; network = null;
subscriberId = null; subscriberId = null;
networkId = null; networkId = null;
legacyNetworkType = 0;
} }
public NetworkState(@NonNull NetworkInfo networkInfo, @NonNull LinkProperties linkProperties, public NetworkState(@NonNull NetworkInfo networkInfo, @NonNull LinkProperties linkProperties,
@@ -60,6 +62,8 @@ public class NetworkState implements Parcelable {
this.network = network; this.network = network;
this.subscriberId = subscriberId; this.subscriberId = subscriberId;
this.networkId = networkId; 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 // This object is an atomic view of a network, so the various components
// should always agree on roaming state. // should always agree on roaming state.
@@ -80,6 +84,7 @@ public class NetworkState implements Parcelable {
network = in.readParcelable(null); network = in.readParcelable(null);
subscriberId = in.readString(); subscriberId = in.readString();
networkId = in.readString(); networkId = in.readString();
legacyNetworkType = in.readInt();
} }
@Override @Override
@@ -95,6 +100,7 @@ public class NetworkState implements Parcelable {
out.writeParcelable(network, flags); out.writeParcelable(network, flags);
out.writeString(subscriberId); out.writeString(subscriberId);
out.writeString(networkId); out.writeString(networkId);
out.writeInt(legacyNetworkType);
} }
@UnsupportedAppUsage @UnsupportedAppUsage

View File

@@ -1854,7 +1854,8 @@ public class ConnectivityService extends IConnectivityManager.Stub
final ArrayList<NetworkState> result = new ArrayList<>(); final ArrayList<NetworkState> result = new ArrayList<>();
for (Network network : getAllNetworks()) { for (Network network : getAllNetworks()) {
final NetworkAgentInfo nai = getNetworkAgentInfoForNetwork(network); 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 // TODO (b/73321673) : NetworkState contains a copy of the
// NetworkCapabilities, which may contain UIDs of apps to which the // NetworkCapabilities, which may contain UIDs of apps to which the
// network applies. Should the UIDs be cleared so as not to leak or // network applies. Should the UIDs be cleared so as not to leak or

View File

@@ -62,7 +62,6 @@ class NetworkTemplateTest {
): NetworkState { ): NetworkState {
val info = mock(NetworkInfo::class.java) val info = mock(NetworkInfo::class.java)
doReturn(type).`when`(info).type doReturn(type).`when`(info).type
doReturn(NetworkInfo.State.CONNECTED).`when`(info).state
val lp = LinkProperties() val lp = LinkProperties()
val caps = NetworkCapabilities().apply { val caps = NetworkCapabilities().apply {
setCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED, false) setCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED, false)