Replace the usage of UidRange

UidRange is used in a shared way between ConnectivityService
and VPN through the use of NetworkCapabilities. UidRange will
be part of the ConnectivityService mainline but Vpn.java will
stay in the framework. We need a way to replace the APIs using
UidRange, or to make UidRange system API. The only really
relevant surface here is NetworkCapabilities#{setUids, getUids}.
The need for UidRange could be replaced by an integer Range, so
replace the usage of UidRange by a integer Range in
NetworkCapabilities#{setUids, getUids} and update the relevant
callers.

Bug: 172183305
Test: atest FrameworksNetTests CtsNetTestCasesLatestSdk
Merged-In: I4e5aec6ef1ea02e038fcd7ed117a3b67b69c5cb9
Change-Id: Idb7f353788c5779a4fbbd107595e9326b99fe0a8
This commit is contained in:
Chiachang Wang
2021-02-22 18:36:38 +08:00
committed by Lorenzo Colitti
parent 6863fbab73
commit 545aafdd16
3 changed files with 53 additions and 14 deletions

View File

@@ -33,6 +33,7 @@ import android.os.Parcelable;
import android.os.Process; import android.os.Process;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.ArraySet; import android.util.ArraySet;
import android.util.Range;
import android.util.proto.ProtoOutputStream; import android.util.proto.ProtoOutputStream;
import com.android.internal.annotations.VisibleForTesting; import com.android.internal.annotations.VisibleForTesting;
@@ -216,7 +217,7 @@ public final class NetworkCapabilities implements Parcelable {
setTransportInfo(null); setTransportInfo(null);
} }
mSignalStrength = nc.mSignalStrength; mSignalStrength = nc.mSignalStrength;
setUids(nc.mUids); // Will make the defensive copy mUids = (nc.mUids == null) ? null : new ArraySet<>(nc.mUids);
setAdministratorUids(nc.getAdministratorUids()); setAdministratorUids(nc.getAdministratorUids());
mOwnerUid = nc.mOwnerUid; mOwnerUid = nc.mOwnerUid;
mUnwantedNetworkCapabilities = nc.mUnwantedNetworkCapabilities; mUnwantedNetworkCapabilities = nc.mUnwantedNetworkCapabilities;
@@ -1519,9 +1520,8 @@ public final class NetworkCapabilities implements Parcelable {
* @hide * @hide
*/ */
public @NonNull NetworkCapabilities setSingleUid(int uid) { public @NonNull NetworkCapabilities setSingleUid(int uid) {
final ArraySet<UidRange> identity = new ArraySet<>(1); mUids = new ArraySet<>(1);
identity.add(new UidRange(uid, uid)); mUids.add(new UidRange(uid, uid));
setUids(identity);
return this; return this;
} }
@@ -1530,12 +1530,8 @@ public final class NetworkCapabilities implements Parcelable {
* This makes a copy of the set so that callers can't modify it after the call. * This makes a copy of the set so that callers can't modify it after the call.
* @hide * @hide
*/ */
public @NonNull NetworkCapabilities setUids(Set<UidRange> uids) { public @NonNull NetworkCapabilities setUids(@Nullable Set<Range<Integer>> uids) {
if (null == uids) { mUids = UidRange.fromIntRanges(uids);
mUids = null;
} else {
mUids = new ArraySet<>(uids);
}
return this; return this;
} }
@@ -1544,8 +1540,19 @@ public final class NetworkCapabilities implements Parcelable {
* This returns a copy of the set so that callers can't modify the original object. * This returns a copy of the set so that callers can't modify the original object.
* @hide * @hide
*/ */
public @Nullable Set<UidRange> getUids() { public @Nullable Set<Range<Integer>> getUids() {
return null == mUids ? null : new ArraySet<>(mUids); return UidRange.toIntRanges(mUids);
}
/**
* Get the list of UIDs this network applies to.
* This returns a copy of the set so that callers can't modify the original object.
* @hide
*/
public @Nullable Set<UidRange> getUidRanges() {
if (mUids == null) return null;
return new ArraySet<>(mUids);
} }
/** /**

View File

@@ -45,6 +45,7 @@ import android.os.Parcel;
import android.os.Parcelable; import android.os.Parcelable;
import android.os.Process; import android.os.Process;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Range;
import android.util.proto.ProtoOutputStream; import android.util.proto.ProtoOutputStream;
import java.util.Arrays; import java.util.Arrays;
@@ -277,11 +278,11 @@ public class NetworkRequest implements Parcelable {
* Set the watched UIDs for this request. This will be reset and wiped out unless * Set the watched UIDs for this request. This will be reset and wiped out unless
* the calling app holds the CHANGE_NETWORK_STATE permission. * the calling app holds the CHANGE_NETWORK_STATE permission.
* *
* @param uids The watched UIDs as a set of UidRanges, or null for everything. * @param uids The watched UIDs as a set of {@code Range<Integer>}, or null for everything.
* @return The builder to facilitate chaining. * @return The builder to facilitate chaining.
* @hide * @hide
*/ */
public Builder setUids(Set<UidRange> uids) { public Builder setUids(@Nullable Set<Range<Integer>> uids) {
mNetworkCapabilities.setUids(uids); mNetworkCapabilities.setUids(uids);
return this; return this;
} }

View File

@@ -20,8 +20,11 @@ import android.annotation.Nullable;
import android.os.Parcel; import android.os.Parcel;
import android.os.Parcelable; import android.os.Parcelable;
import android.os.UserHandle; import android.os.UserHandle;
import android.util.ArraySet;
import android.util.Range;
import java.util.Collection; import java.util.Collection;
import java.util.Set;
/** /**
* An inclusive range of UIDs. * An inclusive range of UIDs.
@@ -149,4 +152,32 @@ public final class UidRange implements Parcelable {
} }
return false; return false;
} }
/**
* Convert a set of {@code Range<Integer>} to a set of {@link UidRange}.
*/
@Nullable
public static ArraySet<UidRange> fromIntRanges(@Nullable Set<Range<Integer>> ranges) {
if (null == ranges) return null;
final ArraySet<UidRange> uids = new ArraySet<>();
for (Range<Integer> range : ranges) {
uids.add(new UidRange(range.getLower(), range.getUpper()));
}
return uids;
}
/**
* Convert a set of {@link UidRange} to a set of {@code Range<Integer>}.
*/
@Nullable
public static ArraySet<Range<Integer>> toIntRanges(@Nullable Set<UidRange> ranges) {
if (null == ranges) return null;
final ArraySet<Range<Integer>> uids = new ArraySet<>();
for (UidRange range : ranges) {
uids.add(new Range<Integer>(range.start, range.stop));
}
return uids;
}
} }