Genericize NC#hasSameUids

This will be used by another set of UIDs in a future patch

Test: FrameworksNetTests
Change-Id: I2c5d18ef93e73b702723814592ef3f3baf5dfbc4
This commit is contained in:
Chalard Jean
2021-12-13 21:37:12 +09:00
parent 286c0e5336
commit f4802fa4c2
4 changed files with 88 additions and 25 deletions

View File

@@ -1591,28 +1591,6 @@ public final class NetworkCapabilities implements Parcelable {
return false;
}
/**
* Compare if the given NetworkCapabilities have the same UIDs.
*
* @hide
*/
public static boolean hasSameUids(@Nullable NetworkCapabilities nc1,
@Nullable NetworkCapabilities nc2) {
final Set<UidRange> uids1 = (nc1 == null) ? null : nc1.mUids;
final Set<UidRange> uids2 = (nc2 == null) ? null : nc2.mUids;
if (null == uids1) return null == uids2;
if (null == uids2) return false;
// Make a copy so it can be mutated to check that all ranges in uids2 also are in uids.
final Set<UidRange> uids = new ArraySet<>(uids2);
for (UidRange range : uids1) {
if (!uids.contains(range)) {
return false;
}
uids.remove(range);
}
return uids.isEmpty();
}
/**
* Tests if the set of UIDs that this network applies to is the same as the passed network.
* <p>
@@ -1623,13 +1601,13 @@ public final class NetworkCapabilities implements Parcelable {
* Note that this method is not very optimized, which is fine as long as it's not used very
* often.
* <p>
* nc is assumed nonnull.
* nc is assumed nonnull, else NPE.
*
* @hide
*/
@VisibleForTesting
public boolean equalsUids(@NonNull NetworkCapabilities nc) {
return hasSameUids(nc, this);
return UidRange.hasSameUids(nc.mUids, mUids);
}
/**

View File

@@ -180,4 +180,24 @@ public final class UidRange implements Parcelable {
}
return uids;
}
/**
* Compare if the given UID range sets have the same UIDs.
*
* @hide
*/
public static boolean hasSameUids(@Nullable Set<UidRange> uids1,
@Nullable Set<UidRange> uids2) {
if (null == uids1) return null == uids2;
if (null == uids2) return false;
// Make a copy so it can be mutated to check that all ranges in uids2 also are in uids.
final Set<UidRange> remainingUids = new ArraySet<>(uids2);
for (UidRange range : uids1) {
if (!remainingUids.contains(range)) {
return false;
}
remainingUids.remove(range);
}
return remainingUids.isEmpty();
}
}