Add new APIs in NetworkCapabilities to set and get underlying networks
Previously, the caller can only know about the transport type of the underlying network. The information might not be enough if the device support WiFi STA+STA. Thus, provide an API for the caller to get the correct underlying network. Bug: 191918368 Test: atest FrameworksNetTests:NetworkCapabilitiesTest Change-Id: I7752b2356770f4572f6ca4cbaecaa45c09d6d72f
This commit is contained in:
@@ -42,7 +42,10 @@ import com.android.net.module.util.NetworkCapabilitiesUtils;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.StringJoiner;
|
||||
@@ -129,6 +132,11 @@ public final class NetworkCapabilities implements Parcelable {
|
||||
// Set to true when private DNS is broken.
|
||||
private boolean mPrivateDnsBroken;
|
||||
|
||||
// Underlying networks, if any. VPNs and VCNs typically have underlying networks.
|
||||
// This is an unmodifiable list and it will be returned as is in the getter.
|
||||
@Nullable
|
||||
private List<Network> mUnderlyingNetworks;
|
||||
|
||||
/**
|
||||
* Uid of the app making the request.
|
||||
*/
|
||||
@@ -184,6 +192,7 @@ public final class NetworkCapabilities implements Parcelable {
|
||||
mRequestorUid = Process.INVALID_UID;
|
||||
mRequestorPackageName = null;
|
||||
mSubIds = new ArraySet<>();
|
||||
mUnderlyingNetworks = null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -213,6 +222,9 @@ public final class NetworkCapabilities implements Parcelable {
|
||||
mRequestorUid = nc.mRequestorUid;
|
||||
mRequestorPackageName = nc.mRequestorPackageName;
|
||||
mSubIds = new ArraySet<>(nc.mSubIds);
|
||||
// mUnderlyingNetworks is an unmodifiable list if non-null, so a defensive copy is not
|
||||
// necessary.
|
||||
mUnderlyingNetworks = nc.mUnderlyingNetworks;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -698,6 +710,41 @@ public final class NetworkCapabilities implements Parcelable {
|
||||
setCapabilities(capabilities, new int[] {});
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the underlying networks of this network.
|
||||
*
|
||||
* @param networks The underlying networks of this network.
|
||||
*
|
||||
* @hide
|
||||
*/
|
||||
public void setUnderlyingNetworks(@Nullable List<Network> networks) {
|
||||
mUnderlyingNetworks =
|
||||
(networks == null) ? null : Collections.unmodifiableList(new ArrayList<>(networks));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the underlying networks of this network. If the caller is not system privileged, this is
|
||||
* always redacted to null and it will be never useful to the caller.
|
||||
*
|
||||
* @return <li>If the list is null, this network hasn't declared underlying networks.</li>
|
||||
* <li>If the list is empty, this network has declared that it has no underlying
|
||||
* networks or it doesn't run on any of the available networks.</li>
|
||||
* <li>The list can contain multiple underlying networks, e.g. a VPN running over
|
||||
* multiple networks at the same time.</li>
|
||||
*
|
||||
* @hide
|
||||
*/
|
||||
@SuppressLint("NullableCollection")
|
||||
@Nullable
|
||||
@SystemApi
|
||||
public List<Network> getUnderlyingNetworks() {
|
||||
return mUnderlyingNetworks;
|
||||
}
|
||||
|
||||
private boolean equalsUnderlyingNetworks(@NonNull NetworkCapabilities nc) {
|
||||
return Objects.equals(getUnderlyingNetworks(), nc.getUnderlyingNetworks());
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests for the presence of a capability on this instance.
|
||||
*
|
||||
@@ -1901,7 +1948,8 @@ public final class NetworkCapabilities implements Parcelable {
|
||||
&& equalsPrivateDnsBroken(that)
|
||||
&& equalsRequestor(that)
|
||||
&& equalsAdministratorUids(that)
|
||||
&& equalsSubscriptionIds(that);
|
||||
&& equalsSubscriptionIds(that)
|
||||
&& equalsUnderlyingNetworks(that);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -1924,7 +1972,8 @@ public final class NetworkCapabilities implements Parcelable {
|
||||
+ Objects.hashCode(mRequestorUid) * 53
|
||||
+ Objects.hashCode(mRequestorPackageName) * 59
|
||||
+ Arrays.hashCode(mAdministratorUids) * 61
|
||||
+ Objects.hashCode(mSubIds) * 67;
|
||||
+ Objects.hashCode(mSubIds) * 67
|
||||
+ Objects.hashCode(mUnderlyingNetworks) * 71;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -1959,6 +2008,7 @@ public final class NetworkCapabilities implements Parcelable {
|
||||
dest.writeInt(mRequestorUid);
|
||||
dest.writeString(mRequestorPackageName);
|
||||
dest.writeIntArray(CollectionUtils.toIntArray(mSubIds));
|
||||
dest.writeTypedList(mUnderlyingNetworks);
|
||||
}
|
||||
|
||||
public static final @android.annotation.NonNull Creator<NetworkCapabilities> CREATOR =
|
||||
@@ -1987,6 +2037,7 @@ public final class NetworkCapabilities implements Parcelable {
|
||||
for (int i = 0; i < subIdInts.length; i++) {
|
||||
netCap.mSubIds.add(subIdInts[i]);
|
||||
}
|
||||
netCap.setUnderlyingNetworks(in.createTypedArrayList(Network.CREATOR));
|
||||
return netCap;
|
||||
}
|
||||
@Override
|
||||
@@ -2078,6 +2129,16 @@ public final class NetworkCapabilities implements Parcelable {
|
||||
sb.append(" SubscriptionIds: ").append(mSubIds);
|
||||
}
|
||||
|
||||
if (mUnderlyingNetworks != null && mUnderlyingNetworks.size() > 0) {
|
||||
sb.append(" Underlying networks: [");
|
||||
final StringJoiner joiner = new StringJoiner(",");
|
||||
for (int i = 0; i < mUnderlyingNetworks.size(); i++) {
|
||||
joiner.add(mUnderlyingNetworks.get(i).toString());
|
||||
}
|
||||
sb.append(joiner.toString());
|
||||
sb.append("]");
|
||||
}
|
||||
|
||||
sb.append("]");
|
||||
return sb.toString();
|
||||
}
|
||||
@@ -2795,6 +2856,17 @@ public final class NetworkCapabilities implements Parcelable {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the underlying networks of this network.
|
||||
*
|
||||
* @param networks The underlying networks of this network.
|
||||
*/
|
||||
@NonNull
|
||||
public Builder setUnderlyingNetworks(@Nullable List<Network> networks) {
|
||||
mCaps.setUnderlyingNetworks(networks);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the instance of the capabilities.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user