Merge "[VCN15] expose addUnwantedCapability and related APIs" into sc-dev
This commit is contained in:
committed by
Android (Google) Code Review
commit
bd2dcae9a3
@@ -45,6 +45,7 @@ package android.net {
|
||||
public final class NetworkCapabilities implements android.os.Parcelable {
|
||||
ctor public NetworkCapabilities(@Nullable android.net.NetworkCapabilities, long);
|
||||
method @Nullable public java.util.Set<android.util.Range<java.lang.Integer>> getUids();
|
||||
method public boolean hasUnwantedCapability(int);
|
||||
field public static final long REDACT_ALL = -1L; // 0xffffffffffffffffL
|
||||
field public static final long REDACT_FOR_ACCESS_FINE_LOCATION = 1L; // 0x1L
|
||||
field public static final long REDACT_FOR_LOCAL_MAC_ADDRESS = 2L; // 0x2L
|
||||
@@ -57,7 +58,13 @@ package android.net {
|
||||
method @NonNull public android.net.NetworkCapabilities.Builder setUids(@Nullable java.util.Set<android.util.Range<java.lang.Integer>>);
|
||||
}
|
||||
|
||||
public class NetworkRequest implements android.os.Parcelable {
|
||||
method public boolean hasUnwantedCapability(int);
|
||||
}
|
||||
|
||||
public static class NetworkRequest.Builder {
|
||||
method @NonNull public android.net.NetworkRequest.Builder addUnwantedCapability(int);
|
||||
method @NonNull public android.net.NetworkRequest.Builder removeUnwantedCapability(int);
|
||||
method @NonNull public android.net.NetworkRequest.Builder setUids(@Nullable java.util.Set<android.util.Range<java.lang.Integer>>);
|
||||
}
|
||||
|
||||
|
||||
@@ -639,19 +639,31 @@ public final class NetworkCapabilities implements Parcelable {
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes (if found) the given capability from this {@code NetworkCapability} instance.
|
||||
* Removes (if found) the given capability from this {@code NetworkCapability}
|
||||
* instance that were added via addCapability(int) or setCapabilities(int[], int[]).
|
||||
*
|
||||
* @param capability the capability to be removed.
|
||||
* @return This NetworkCapabilities instance, to facilitate chaining.
|
||||
* @hide
|
||||
*/
|
||||
public @NonNull NetworkCapabilities removeCapability(@NetCapability int capability) {
|
||||
// Note that this method removes capabilities that were added via addCapability(int),
|
||||
// addUnwantedCapability(int) or setCapabilities(int[], int[]).
|
||||
checkValidCapability(capability);
|
||||
final long mask = ~(1 << capability);
|
||||
mNetworkCapabilities &= mask;
|
||||
mUnwantedNetworkCapabilities &= mask;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes (if found) the given unwanted capability from this {@code NetworkCapability}
|
||||
* instance that were added via addUnwantedCapability(int) or setCapabilities(int[], int[]).
|
||||
*
|
||||
* @param capability the capability to be removed.
|
||||
* @return This NetworkCapabilities instance, to facilitate chaining.
|
||||
* @hide
|
||||
*/
|
||||
public @NonNull NetworkCapabilities removeUnwantedCapability(@NetCapability int capability) {
|
||||
checkValidCapability(capability);
|
||||
mUnwantedNetworkCapabilities &= ~(1 << capability);
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -723,6 +735,7 @@ public final class NetworkCapabilities implements Parcelable {
|
||||
}
|
||||
|
||||
/** @hide */
|
||||
@SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
|
||||
public boolean hasUnwantedCapability(@NetCapability int capability) {
|
||||
return isValidCapability(capability)
|
||||
&& ((mUnwantedNetworkCapabilities & (1 << capability)) != 0);
|
||||
@@ -736,10 +749,16 @@ public final class NetworkCapabilities implements Parcelable {
|
||||
return ((mNetworkCapabilities & CONNECTIVITY_MANAGED_CAPABILITIES) != 0);
|
||||
}
|
||||
|
||||
/** Note this method may result in having the same capability in wanted and unwanted lists. */
|
||||
private void combineNetCapabilities(@NonNull NetworkCapabilities nc) {
|
||||
this.mNetworkCapabilities |= nc.mNetworkCapabilities;
|
||||
this.mUnwantedNetworkCapabilities |= nc.mUnwantedNetworkCapabilities;
|
||||
final long wantedCaps = this.mNetworkCapabilities | nc.mNetworkCapabilities;
|
||||
final long unwantedCaps =
|
||||
this.mUnwantedNetworkCapabilities | nc.mUnwantedNetworkCapabilities;
|
||||
if ((wantedCaps & unwantedCaps) != 0) {
|
||||
throw new IllegalArgumentException(
|
||||
"Cannot have the same capability in wanted and unwanted lists.");
|
||||
}
|
||||
this.mNetworkCapabilities = wantedCaps;
|
||||
this.mUnwantedNetworkCapabilities = unwantedCaps;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -313,11 +313,30 @@ public class NetworkRequest implements Parcelable {
|
||||
*
|
||||
* @hide
|
||||
*/
|
||||
@NonNull
|
||||
@SuppressLint("MissingGetterMatchingBuilder")
|
||||
@SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
|
||||
public Builder addUnwantedCapability(@NetworkCapabilities.NetCapability int capability) {
|
||||
mNetworkCapabilities.addUnwantedCapability(capability);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes (if found) the given unwanted capability from this builder instance.
|
||||
*
|
||||
* @param capability The unwanted capability to remove.
|
||||
* @return The builder to facilitate chaining.
|
||||
*
|
||||
* @hide
|
||||
*/
|
||||
@NonNull
|
||||
@SuppressLint("BuilderSetStyle")
|
||||
@SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
|
||||
public Builder removeUnwantedCapability(@NetworkCapabilities.NetCapability int capability) {
|
||||
mNetworkCapabilities.removeUnwantedCapability(capability);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Completely clears all the {@code NetworkCapabilities} from this builder instance,
|
||||
* removing even the capabilities that are set by default when the object is constructed.
|
||||
@@ -575,6 +594,7 @@ public class NetworkRequest implements Parcelable {
|
||||
*
|
||||
* @hide
|
||||
*/
|
||||
@SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
|
||||
public boolean hasUnwantedCapability(@NetCapability int capability) {
|
||||
return networkCapabilities.hasUnwantedCapability(capability);
|
||||
}
|
||||
|
||||
@@ -531,11 +531,22 @@ public class NetworkCapabilitiesTest {
|
||||
assertFalse(nc1.equalsNetCapabilities(nc2));
|
||||
nc2.addUnwantedCapability(NET_CAPABILITY_INTERNET);
|
||||
assertTrue(nc1.equalsNetCapabilities(nc2));
|
||||
if (isAtLeastS()) {
|
||||
// Remove a required capability doesn't affect unwanted capabilities.
|
||||
// This is a behaviour change from S.
|
||||
nc1.removeCapability(NET_CAPABILITY_INTERNET);
|
||||
assertTrue(nc1.equalsNetCapabilities(nc2));
|
||||
|
||||
nc1.removeCapability(NET_CAPABILITY_INTERNET);
|
||||
assertFalse(nc1.equalsNetCapabilities(nc2));
|
||||
nc2.removeCapability(NET_CAPABILITY_INTERNET);
|
||||
assertTrue(nc1.equalsNetCapabilities(nc2));
|
||||
nc1.removeUnwantedCapability(NET_CAPABILITY_INTERNET);
|
||||
assertFalse(nc1.equalsNetCapabilities(nc2));
|
||||
nc2.removeUnwantedCapability(NET_CAPABILITY_INTERNET);
|
||||
assertTrue(nc1.equalsNetCapabilities(nc2));
|
||||
} else {
|
||||
nc1.removeCapability(NET_CAPABILITY_INTERNET);
|
||||
assertFalse(nc1.equalsNetCapabilities(nc2));
|
||||
nc2.removeCapability(NET_CAPABILITY_INTERNET);
|
||||
assertTrue(nc1.equalsNetCapabilities(nc2));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -596,11 +607,20 @@ public class NetworkCapabilitiesTest {
|
||||
// This will effectively move NOT_ROAMING capability from required to unwanted for nc1.
|
||||
nc1.addUnwantedCapability(NET_CAPABILITY_NOT_ROAMING);
|
||||
|
||||
nc2.combineCapabilities(nc1);
|
||||
// We will get this capability in both requested and unwanted lists thus this request
|
||||
// will never be satisfied.
|
||||
assertTrue(nc2.hasCapability(NET_CAPABILITY_NOT_ROAMING));
|
||||
assertTrue(nc2.hasUnwantedCapability(NET_CAPABILITY_NOT_ROAMING));
|
||||
if (isAtLeastS()) {
|
||||
// From S, it is not allowed to have the same capability in both wanted and
|
||||
// unwanted list.
|
||||
assertThrows(IllegalArgumentException.class, () -> nc2.combineCapabilities(nc1));
|
||||
} else {
|
||||
nc2.combineCapabilities(nc1);
|
||||
// We will get this capability in both requested and unwanted lists thus this request
|
||||
// will never be satisfied.
|
||||
assertTrue(nc2.hasCapability(NET_CAPABILITY_NOT_ROAMING));
|
||||
assertTrue(nc2.hasUnwantedCapability(NET_CAPABILITY_NOT_ROAMING));
|
||||
}
|
||||
|
||||
// Remove unwanted capability to continue other tests.
|
||||
nc1.removeUnwantedCapability(NET_CAPABILITY_NOT_ROAMING);
|
||||
|
||||
nc1.setSSID(TEST_SSID);
|
||||
nc2.combineCapabilities(nc1);
|
||||
|
||||
Reference in New Issue
Block a user