Merge "[VCN15] expose addUnwantedCapability and related APIs" am: 20d187254b
Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/1639654 Change-Id: Ie88ff422c473d3891a054fa75b5175b30c10cec9
This commit is contained in:
@@ -42,6 +42,7 @@ package android.net {
|
|||||||
public final class NetworkCapabilities implements android.os.Parcelable {
|
public final class NetworkCapabilities implements android.os.Parcelable {
|
||||||
ctor public NetworkCapabilities(@Nullable android.net.NetworkCapabilities, long);
|
ctor public NetworkCapabilities(@Nullable android.net.NetworkCapabilities, long);
|
||||||
method @Nullable public java.util.Set<android.util.Range<java.lang.Integer>> getUids();
|
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_ALL = -1L; // 0xffffffffffffffffL
|
||||||
field public static final long REDACT_FOR_ACCESS_FINE_LOCATION = 1L; // 0x1L
|
field public static final long REDACT_FOR_ACCESS_FINE_LOCATION = 1L; // 0x1L
|
||||||
field public static final long REDACT_FOR_LOCAL_MAC_ADDRESS = 2L; // 0x2L
|
field public static final long REDACT_FOR_LOCAL_MAC_ADDRESS = 2L; // 0x2L
|
||||||
@@ -54,7 +55,13 @@ package android.net {
|
|||||||
method @NonNull public android.net.NetworkCapabilities.Builder setUids(@Nullable java.util.Set<android.util.Range<java.lang.Integer>>);
|
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 {
|
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>>);
|
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.
|
* @param capability the capability to be removed.
|
||||||
* @return This NetworkCapabilities instance, to facilitate chaining.
|
* @return This NetworkCapabilities instance, to facilitate chaining.
|
||||||
* @hide
|
* @hide
|
||||||
*/
|
*/
|
||||||
public @NonNull NetworkCapabilities removeCapability(@NetCapability int capability) {
|
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);
|
checkValidCapability(capability);
|
||||||
final long mask = ~(1 << capability);
|
final long mask = ~(1 << capability);
|
||||||
mNetworkCapabilities &= mask;
|
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;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -723,6 +735,7 @@ public final class NetworkCapabilities implements Parcelable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** @hide */
|
/** @hide */
|
||||||
|
@SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
|
||||||
public boolean hasUnwantedCapability(@NetCapability int capability) {
|
public boolean hasUnwantedCapability(@NetCapability int capability) {
|
||||||
return isValidCapability(capability)
|
return isValidCapability(capability)
|
||||||
&& ((mUnwantedNetworkCapabilities & (1 << capability)) != 0);
|
&& ((mUnwantedNetworkCapabilities & (1 << capability)) != 0);
|
||||||
@@ -736,10 +749,16 @@ public final class NetworkCapabilities implements Parcelable {
|
|||||||
return ((mNetworkCapabilities & CONNECTIVITY_MANAGED_CAPABILITIES) != 0);
|
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) {
|
private void combineNetCapabilities(@NonNull NetworkCapabilities nc) {
|
||||||
this.mNetworkCapabilities |= nc.mNetworkCapabilities;
|
final long wantedCaps = this.mNetworkCapabilities | nc.mNetworkCapabilities;
|
||||||
this.mUnwantedNetworkCapabilities |= nc.mUnwantedNetworkCapabilities;
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -305,11 +305,30 @@ public class NetworkRequest implements Parcelable {
|
|||||||
*
|
*
|
||||||
* @hide
|
* @hide
|
||||||
*/
|
*/
|
||||||
|
@NonNull
|
||||||
|
@SuppressLint("MissingGetterMatchingBuilder")
|
||||||
|
@SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
|
||||||
public Builder addUnwantedCapability(@NetworkCapabilities.NetCapability int capability) {
|
public Builder addUnwantedCapability(@NetworkCapabilities.NetCapability int capability) {
|
||||||
mNetworkCapabilities.addUnwantedCapability(capability);
|
mNetworkCapabilities.addUnwantedCapability(capability);
|
||||||
return this;
|
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,
|
* Completely clears all the {@code NetworkCapabilities} from this builder instance,
|
||||||
* removing even the capabilities that are set by default when the object is constructed.
|
* removing even the capabilities that are set by default when the object is constructed.
|
||||||
@@ -567,6 +586,7 @@ public class NetworkRequest implements Parcelable {
|
|||||||
*
|
*
|
||||||
* @hide
|
* @hide
|
||||||
*/
|
*/
|
||||||
|
@SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
|
||||||
public boolean hasUnwantedCapability(@NetCapability int capability) {
|
public boolean hasUnwantedCapability(@NetCapability int capability) {
|
||||||
return networkCapabilities.hasUnwantedCapability(capability);
|
return networkCapabilities.hasUnwantedCapability(capability);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -531,11 +531,22 @@ public class NetworkCapabilitiesTest {
|
|||||||
assertFalse(nc1.equalsNetCapabilities(nc2));
|
assertFalse(nc1.equalsNetCapabilities(nc2));
|
||||||
nc2.addUnwantedCapability(NET_CAPABILITY_INTERNET);
|
nc2.addUnwantedCapability(NET_CAPABILITY_INTERNET);
|
||||||
assertTrue(nc1.equalsNetCapabilities(nc2));
|
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);
|
nc1.removeUnwantedCapability(NET_CAPABILITY_INTERNET);
|
||||||
assertFalse(nc1.equalsNetCapabilities(nc2));
|
assertFalse(nc1.equalsNetCapabilities(nc2));
|
||||||
nc2.removeCapability(NET_CAPABILITY_INTERNET);
|
nc2.removeUnwantedCapability(NET_CAPABILITY_INTERNET);
|
||||||
assertTrue(nc1.equalsNetCapabilities(nc2));
|
assertTrue(nc1.equalsNetCapabilities(nc2));
|
||||||
|
} else {
|
||||||
|
nc1.removeCapability(NET_CAPABILITY_INTERNET);
|
||||||
|
assertFalse(nc1.equalsNetCapabilities(nc2));
|
||||||
|
nc2.removeCapability(NET_CAPABILITY_INTERNET);
|
||||||
|
assertTrue(nc1.equalsNetCapabilities(nc2));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -596,11 +607,20 @@ public class NetworkCapabilitiesTest {
|
|||||||
// This will effectively move NOT_ROAMING capability from required to unwanted for nc1.
|
// This will effectively move NOT_ROAMING capability from required to unwanted for nc1.
|
||||||
nc1.addUnwantedCapability(NET_CAPABILITY_NOT_ROAMING);
|
nc1.addUnwantedCapability(NET_CAPABILITY_NOT_ROAMING);
|
||||||
|
|
||||||
nc2.combineCapabilities(nc1);
|
if (isAtLeastS()) {
|
||||||
// We will get this capability in both requested and unwanted lists thus this request
|
// From S, it is not allowed to have the same capability in both wanted and
|
||||||
// will never be satisfied.
|
// unwanted list.
|
||||||
assertTrue(nc2.hasCapability(NET_CAPABILITY_NOT_ROAMING));
|
assertThrows(IllegalArgumentException.class, () -> nc2.combineCapabilities(nc1));
|
||||||
assertTrue(nc2.hasUnwantedCapability(NET_CAPABILITY_NOT_ROAMING));
|
} 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);
|
nc1.setSSID(TEST_SSID);
|
||||||
nc2.combineCapabilities(nc1);
|
nc2.combineCapabilities(nc1);
|
||||||
|
|||||||
Reference in New Issue
Block a user