diff --git a/framework/api/current.txt b/framework/api/current.txt index 827da6d817..ce0c86820a 100644 --- a/framework/api/current.txt +++ b/framework/api/current.txt @@ -294,7 +294,7 @@ package android.net { ctor public NetworkCapabilities(android.net.NetworkCapabilities); method public int describeContents(); method @NonNull public int[] getCapabilities(); - method @NonNull public int[] getEnterpriseCapabilitySubLevels(); + method @NonNull public int[] getEnterpriseIds(); method public int getLinkDownstreamBandwidthKbps(); method public int getLinkUpstreamBandwidthKbps(); method @Nullable public android.net.NetworkSpecifier getNetworkSpecifier(); @@ -302,6 +302,7 @@ package android.net { method public int getSignalStrength(); method @Nullable public android.net.TransportInfo getTransportInfo(); method public boolean hasCapability(int); + method public boolean hasEnterpriseId(int); method public boolean hasTransport(int); method public void writeToParcel(android.os.Parcel, int); field @NonNull public static final android.os.Parcelable.Creator CREATOR; @@ -332,6 +333,11 @@ package android.net { field public static final int NET_CAPABILITY_VALIDATED = 16; // 0x10 field public static final int NET_CAPABILITY_WIFI_P2P = 6; // 0x6 field public static final int NET_CAPABILITY_XCAP = 9; // 0x9 + field public static final int NET_ENTERPRISE_ID_1 = 1; // 0x1 + field public static final int NET_ENTERPRISE_ID_2 = 2; // 0x2 + field public static final int NET_ENTERPRISE_ID_3 = 3; // 0x3 + field public static final int NET_ENTERPRISE_ID_4 = 4; // 0x4 + field public static final int NET_ENTERPRISE_ID_5 = 5; // 0x5 field public static final int SIGNAL_STRENGTH_UNSPECIFIED = -2147483648; // 0x80000000 field public static final int TRANSPORT_BLUETOOTH = 2; // 0x2 field public static final int TRANSPORT_CELLULAR = 0; // 0x0 diff --git a/framework/api/module-lib-current.txt b/framework/api/module-lib-current.txt index ec169b608b..db08c48dd0 100644 --- a/framework/api/module-lib-current.txt +++ b/framework/api/module-lib-current.txt @@ -133,11 +133,6 @@ package android.net { public final class NetworkCapabilities implements android.os.Parcelable { method @Nullable public java.util.Set> getUids(); method public boolean hasForbiddenCapability(int); - field public static final int NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_1 = 1; // 0x1 - field public static final int NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_2 = 2; // 0x2 - field public static final int NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_3 = 3; // 0x3 - field public static final int NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_4 = 4; // 0x4 - field public static final int NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_5 = 5; // 0x5 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 @@ -166,6 +161,7 @@ package android.net { method @NonNull public java.util.List getExcludedUids(); method @NonNull public java.util.List getIncludedUids(); method public int getPreference(); + method public int getPreferenceEnterpriseId(); method public void writeToParcel(@NonNull android.os.Parcel, int); field @NonNull public static final android.os.Parcelable.Creator CREATOR; } @@ -176,6 +172,7 @@ package android.net { method @NonNull public android.net.ProfileNetworkPreference.Builder setExcludedUids(@Nullable java.util.List); method @NonNull public android.net.ProfileNetworkPreference.Builder setIncludedUids(@Nullable java.util.List); method @NonNull public android.net.ProfileNetworkPreference.Builder setPreference(int); + method @NonNull public android.net.ProfileNetworkPreference.Builder setPreferenceEnterpriseId(int); } public final class TestNetworkInterface implements android.os.Parcelable { diff --git a/framework/api/system-current.txt b/framework/api/system-current.txt index 8d084e671c..b4b35886a1 100644 --- a/framework/api/system-current.txt +++ b/framework/api/system-current.txt @@ -294,11 +294,11 @@ package android.net { ctor public NetworkCapabilities.Builder(); ctor public NetworkCapabilities.Builder(@NonNull android.net.NetworkCapabilities); method @NonNull public android.net.NetworkCapabilities.Builder addCapability(int); - method @NonNull public android.net.NetworkCapabilities.Builder addEnterpriseCapabilitySubLevel(int); + method @NonNull public android.net.NetworkCapabilities.Builder addEnterpriseId(int); method @NonNull public android.net.NetworkCapabilities.Builder addTransportType(int); method @NonNull public android.net.NetworkCapabilities build(); method @NonNull public android.net.NetworkCapabilities.Builder removeCapability(int); - method @NonNull public android.net.NetworkCapabilities.Builder removeEnterpriseCapabilitySubLevel(int); + method @NonNull public android.net.NetworkCapabilities.Builder removeEnterpriseId(int); method @NonNull public android.net.NetworkCapabilities.Builder removeTransportType(int); method @NonNull @RequiresPermission(android.Manifest.permission.NETWORK_FACTORY) public android.net.NetworkCapabilities.Builder setAdministratorUids(@NonNull int[]); method @NonNull public android.net.NetworkCapabilities.Builder setLinkDownstreamBandwidthKbps(int); diff --git a/framework/src/android/net/ConnectivityManager.java b/framework/src/android/net/ConnectivityManager.java index 009890cf3e..75934822c7 100644 --- a/framework/src/android/net/ConnectivityManager.java +++ b/framework/src/android/net/ConnectivityManager.java @@ -16,6 +16,7 @@ package android.net; import static android.annotation.SystemApi.Client.MODULE_LIBRARIES; +import static android.net.NetworkCapabilities.NET_ENTERPRISE_ID_1; import static android.net.NetworkRequest.Type.BACKGROUND_REQUEST; import static android.net.NetworkRequest.Type.LISTEN; import static android.net.NetworkRequest.Type.LISTEN_FOR_BEST; @@ -5537,6 +5538,9 @@ public class ConnectivityManager { ProfileNetworkPreference.Builder preferenceBuilder = new ProfileNetworkPreference.Builder(); preferenceBuilder.setPreference(preference); + if (preference != PROFILE_NETWORK_PREFERENCE_DEFAULT) { + preferenceBuilder.setPreferenceEnterpriseId(NET_ENTERPRISE_ID_1); + } setProfileNetworkPreferences(profile, List.of(preferenceBuilder.build()), executor, listener); } diff --git a/framework/src/android/net/NetworkCapabilities.java b/framework/src/android/net/NetworkCapabilities.java index 84f7cbbd62..c98be471b6 100644 --- a/framework/src/android/net/NetworkCapabilities.java +++ b/framework/src/android/net/NetworkCapabilities.java @@ -16,8 +16,6 @@ package android.net; -import static android.annotation.SystemApi.Client.MODULE_LIBRARIES; - import static com.android.internal.annotations.VisibleForTesting.Visibility.PRIVATE; import android.annotation.IntDef; @@ -149,67 +147,83 @@ public final class NetworkCapabilities implements Parcelable { private String mRequestorPackageName; /** - * enterprise capability sub level 1 - * @hide + * Enterprise capability identifier 1. */ - @SystemApi(client = MODULE_LIBRARIES) - public static final int NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_1 = 1; + public static final int NET_ENTERPRISE_ID_1 = 1; /** - * enterprise capability sub level 2 - * @hide + * Enterprise capability identifier 2. */ - @SystemApi(client = MODULE_LIBRARIES) - public static final int NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_2 = 2; + public static final int NET_ENTERPRISE_ID_2 = 2; /** - * enterprise capability sub level 3 - * @hide + * Enterprise capability identifier 3. */ - @SystemApi(client = MODULE_LIBRARIES) - public static final int NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_3 = 3; + public static final int NET_ENTERPRISE_ID_3 = 3; /** - * enterprise capability sub level 4 - * @hide + * Enterprise capability identifier 4. */ - @SystemApi(client = MODULE_LIBRARIES) - public static final int NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_4 = 4; + public static final int NET_ENTERPRISE_ID_4 = 4; /** - * enterprise capability sub level 5 - * @hide + * Enterprise capability identifier 5. */ - @SystemApi(client = MODULE_LIBRARIES) - public static final int NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_5 = 5; + public static final int NET_ENTERPRISE_ID_5 = 5; /** @hide */ @Retention(RetentionPolicy.SOURCE) @IntDef(prefix = { "NET_CAPABILITY_ENTERPRISE_SUB_LEVEL" }, value = { - NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_1, - NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_2, - NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_3, - NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_4, - NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_5, + NET_ENTERPRISE_ID_1, + NET_ENTERPRISE_ID_2, + NET_ENTERPRISE_ID_3, + NET_ENTERPRISE_ID_4, + NET_ENTERPRISE_ID_5, }) - public @interface EnterpriseCapabilitySubLevel { + public @interface EnterpriseId { } /** - * Bitfield representing the network's enterprise capability sublevel. If any are specified + * Bitfield representing the network's enterprise capability identifier. If any are specified * they will be satisfied by any Network that matches all of them. - * {@see addEnterpriseCapabilitySubLevel} for details on how masks are added + * {@see addEnterpriseId} for details on how masks are added */ - private int mEnterpriseCapabilitySubLevel; + private int mEnterpriseId; /** - * @return all the enteprise capabilities sub level set on this {@code NetworkCapability} - * instance. + * Get enteprise identifiers set. + * + * Get all the enterprise capabilities identifier set on this {@code NetworkCapability} + * If NET_CAPABILITY_ENTERPRISE is set and no enterprise ID is set, it is + * considered to have NET_CAPABILITY_ENTERPRISE by default. + * @return all the enterprise capabilities identifier set. * */ - public @NonNull @EnterpriseCapabilitySubLevel int[] getEnterpriseCapabilitySubLevels() { - return NetworkCapabilitiesUtils.unpackBits(mEnterpriseCapabilitySubLevel); + public @NonNull @EnterpriseId int[] getEnterpriseIds() { + if (hasCapability(NET_CAPABILITY_ENTERPRISE) && mEnterpriseId == 0) { + return new int[]{NET_ENTERPRISE_ID_1}; + } + return NetworkCapabilitiesUtils.unpackBits(mEnterpriseId); + } + + /** + * Tests for the presence of an enterprise capability identifier on this instance. + * + * If NET_CAPABILITY_ENTERPRISE is set and no enterprise ID is set, it is + * considered to have NET_CAPABILITY_ENTERPRISE by default. + * @param enterpriseId the enterprise capability identifier to be tested for. + * @return {@code true} if set on this instance. + */ + public boolean hasEnterpriseId( + @EnterpriseId int enterpriseId) { + if (enterpriseId == NET_ENTERPRISE_ID_1) { + if (hasCapability(NET_CAPABILITY_ENTERPRISE) && mEnterpriseId == 0) { + return true; + } + } + return isValidEnterpriseId(enterpriseId) + && ((mEnterpriseId & (1L << enterpriseId)) != 0); } public NetworkCapabilities() { @@ -258,7 +272,7 @@ public final class NetworkCapabilities implements Parcelable { mRequestorPackageName = null; mSubIds = new ArraySet<>(); mUnderlyingNetworks = null; - mEnterpriseCapabilitySubLevel = 0; + mEnterpriseId = 0; } /** @@ -291,7 +305,7 @@ public final class NetworkCapabilities implements Parcelable { // mUnderlyingNetworks is an unmodifiable list if non-null, so a defensive copy is not // necessary. mUnderlyingNetworks = nc.mUnderlyingNetworks; - mEnterpriseCapabilitySubLevel = nc.mEnterpriseCapabilitySubLevel; + mEnterpriseId = nc.mEnterpriseId; } /** @@ -784,34 +798,34 @@ public final class NetworkCapabilities implements Parcelable { } /** - * Adds the given enterprise capability sub level to this {@code NetworkCapability} instance. - * Note that when searching for a network to satisfy a request, all capabilities sub level + * Adds the given enterprise capability identifier to this {@code NetworkCapability} instance. + * Note that when searching for a network to satisfy a request, all capabilities identifier * requested must be satisfied. * - * @param enterpriseCapabilitySubLevel the enterprise capability sub level to be added. + * @param enterpriseId the enterprise capability identifier to be added. * @return This NetworkCapabilities instance, to facilitate chaining. * @hide */ - private @NonNull NetworkCapabilities addEnterpriseCapabilitySubLevel( - @EnterpriseCapabilitySubLevel int enterpriseCapabilitySubLevel) { - checkValidEnterpriseCapabilitySublevel(enterpriseCapabilitySubLevel); - mEnterpriseCapabilitySubLevel |= 1 << enterpriseCapabilitySubLevel; + public @NonNull NetworkCapabilities addEnterpriseId( + @EnterpriseId int enterpriseId) { + checkValidEnterpriseId(enterpriseId); + mEnterpriseId |= 1 << enterpriseId; return this; } /** - * Removes (if found) the given enterprise capability sublevel from this - * {@code NetworkCapability} instance that were added via addEnterpriseCapabilitySubLevel(int) + * Removes (if found) the given enterprise capability identifier from this + * {@code NetworkCapability} instance that were added via addEnterpriseId(int) * - * @param enterpriseCapabilitySubLevel the enterprise capability sublevel to be removed. + * @param enterpriseId the enterprise capability identifier to be removed. * @return This NetworkCapabilities instance, to facilitate chaining. * @hide */ - private @NonNull NetworkCapabilities removeEnterpriseCapabilitySubLevel( - @EnterpriseCapabilitySubLevel int enterpriseCapabilitySubLevel) { - checkValidEnterpriseCapabilitySublevel(enterpriseCapabilitySubLevel); - final int mask = ~(1 << enterpriseCapabilitySubLevel); - mEnterpriseCapabilitySubLevel &= mask; + private @NonNull NetworkCapabilities removeEnterpriseId( + @EnterpriseId int enterpriseId) { + checkValidEnterpriseId(enterpriseId); + final int mask = ~(1 << enterpriseId); + mEnterpriseId &= mask; return this; } @@ -915,16 +929,19 @@ public final class NetworkCapabilities implements Parcelable { return null; } - private boolean equalsEnterpriseCapabilitiesSubLevel(@NonNull NetworkCapabilities nc) { - return nc.mEnterpriseCapabilitySubLevel == this.mEnterpriseCapabilitySubLevel; + private boolean equalsEnterpriseCapabilitiesId(@NonNull NetworkCapabilities nc) { + return nc.mEnterpriseId == this.mEnterpriseId; } - private boolean satisfiedByEnterpriseCapabilitiesSubLevel(@NonNull NetworkCapabilities nc) { - final int requestedEnterpriseCapabilitiesSubLevel = mEnterpriseCapabilitySubLevel; - final int providedEnterpriseCapabailitiesSubLevel = nc.mEnterpriseCapabilitySubLevel; + private boolean satisfiedByEnterpriseCapabilitiesId(@NonNull NetworkCapabilities nc) { + final int requestedEnterpriseCapabilitiesId = mEnterpriseId; + final int providedEnterpriseCapabailitiesId = nc.mEnterpriseId; - if ((providedEnterpriseCapabailitiesSubLevel & requestedEnterpriseCapabilitiesSubLevel) - == requestedEnterpriseCapabilitiesSubLevel) { + if ((providedEnterpriseCapabailitiesId & requestedEnterpriseCapabilitiesId) + == requestedEnterpriseCapabilitiesId) { + return true; + } else if (providedEnterpriseCapabailitiesId == 0 + && (requestedEnterpriseCapabilitiesId == (1L << NET_ENTERPRISE_ID_1))) { return true; } else { return false; @@ -1836,7 +1853,7 @@ public final class NetworkCapabilities implements Parcelable { && satisfiedByTransportTypes(nc) && (onlyImmutable || satisfiedByLinkBandwidths(nc)) && satisfiedBySpecifier(nc) - && satisfiedByEnterpriseCapabilitiesSubLevel(nc) + && satisfiedByEnterpriseCapabilitiesId(nc) && (onlyImmutable || satisfiedBySignalStrength(nc)) && (onlyImmutable || satisfiedByUids(nc)) && (onlyImmutable || satisfiedBySSID(nc)) @@ -1940,7 +1957,7 @@ public final class NetworkCapabilities implements Parcelable { && equalsAdministratorUids(that) && equalsSubscriptionIds(that) && equalsUnderlyingNetworks(that) - && equalsEnterpriseCapabilitiesSubLevel(that); + && equalsEnterpriseCapabilitiesId(that); } @Override @@ -1965,7 +1982,7 @@ public final class NetworkCapabilities implements Parcelable { + Arrays.hashCode(mAdministratorUids) * 61 + Objects.hashCode(mSubIds) * 67 + Objects.hashCode(mUnderlyingNetworks) * 71 - + mEnterpriseCapabilitySubLevel * 73; + + mEnterpriseId * 73; } @Override @@ -2001,7 +2018,7 @@ public final class NetworkCapabilities implements Parcelable { dest.writeString(mRequestorPackageName); dest.writeIntArray(CollectionUtils.toIntArray(mSubIds)); dest.writeTypedList(mUnderlyingNetworks); - dest.writeInt(mEnterpriseCapabilitySubLevel); + dest.writeInt(mEnterpriseId); } public static final @android.annotation.NonNull Creator CREATOR = @@ -2031,7 +2048,7 @@ public final class NetworkCapabilities implements Parcelable { netCap.mSubIds.add(subIdInts[i]); } netCap.setUnderlyingNetworks(in.createTypedArrayList(Network.CREATOR)); - netCap.mEnterpriseCapabilitySubLevel = in.readInt(); + netCap.mEnterpriseId = in.readInt(); return netCap; } @Override @@ -2123,10 +2140,10 @@ public final class NetworkCapabilities implements Parcelable { sb.append(" SubscriptionIds: ").append(mSubIds); } - if (0 != mEnterpriseCapabilitySubLevel) { - sb.append(" EnterpriseCapabilitySublevel: "); - appendStringRepresentationOfBitMaskToStringBuilder(sb, mEnterpriseCapabilitySubLevel, - NetworkCapabilities::enterpriseCapabilitySublevelNameOf, "&"); + if (0 != mEnterpriseId) { + sb.append(" EnterpriseId: "); + appendStringRepresentationOfBitMaskToStringBuilder(sb, mEnterpriseId, + NetworkCapabilities::enterpriseIdNameOf, "&"); } sb.append(" UnderlyingNetworks: "); @@ -2228,7 +2245,7 @@ public final class NetworkCapabilities implements Parcelable { } } - private static @NonNull String enterpriseCapabilitySublevelNameOf( + private static @NonNull String enterpriseIdNameOf( @NetCapability int capability) { return Integer.toString(capability); } @@ -2273,17 +2290,17 @@ public final class NetworkCapabilities implements Parcelable { } } - private static boolean isValidEnterpriseCapabilitySubLevel( - @NetworkCapabilities.EnterpriseCapabilitySubLevel int enterpriseCapabilitySubLevel) { - return enterpriseCapabilitySubLevel >= NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_1 - && enterpriseCapabilitySubLevel <= NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_5; + private static boolean isValidEnterpriseId( + @NetworkCapabilities.EnterpriseId int enterpriseId) { + return enterpriseId >= NET_ENTERPRISE_ID_1 + && enterpriseId <= NET_ENTERPRISE_ID_5; } - private static void checkValidEnterpriseCapabilitySublevel( - @NetworkCapabilities.EnterpriseCapabilitySubLevel int enterpriseCapabilitySubLevel) { - if (!isValidEnterpriseCapabilitySubLevel(enterpriseCapabilitySubLevel)) { - throw new IllegalArgumentException("enterprise capability sublevel " - + enterpriseCapabilitySubLevel + " is out of range"); + private static void checkValidEnterpriseId( + @NetworkCapabilities.EnterpriseId int enterpriseId) { + if (!isValidEnterpriseId(enterpriseId)) { + throw new IllegalArgumentException("enterprise capability identifier " + + enterpriseId + " is out of range"); } } @@ -2613,33 +2630,33 @@ public final class NetworkCapabilities implements Parcelable { } /** - * Adds the given enterprise capability sub level. - * Note that when searching for a network to satisfy a request, all capabilities sub level - * requested must be satisfied. Enterprise capability sub-level is applicable only + * Adds the given enterprise capability identifier. + * Note that when searching for a network to satisfy a request, all capabilities identifier + * requested must be satisfied. Enterprise capability identifier is applicable only * for NET_CAPABILITY_ENTERPRISE capability * - * @param enterpriseCapabilitySubLevel enterprise capability sub-level. + * @param enterpriseId enterprise capability identifier. * * @return this builder */ @NonNull - public Builder addEnterpriseCapabilitySubLevel( - @EnterpriseCapabilitySubLevel int enterpriseCapabilitySubLevel) { - mCaps.addEnterpriseCapabilitySubLevel(enterpriseCapabilitySubLevel); + public Builder addEnterpriseId( + @EnterpriseId int enterpriseId) { + mCaps.addEnterpriseId(enterpriseId); return this; } /** - * Removes the given enterprise capability sub level. Enterprise capability sub-level is + * Removes the given enterprise capability identifier. Enterprise capability identifier is * applicable only for NET_CAPABILITY_ENTERPRISE capability * - * @param enterpriseCapabilitySubLevel the enterprise capability subLevel + * @param enterpriseId the enterprise capability identifier * @return this builder */ @NonNull - public Builder removeEnterpriseCapabilitySubLevel( - @EnterpriseCapabilitySubLevel int enterpriseCapabilitySubLevel) { - mCaps.removeEnterpriseCapabilitySubLevel(enterpriseCapabilitySubLevel); + public Builder removeEnterpriseId( + @EnterpriseId int enterpriseId) { + mCaps.removeEnterpriseId(enterpriseId); return this; } @@ -2902,10 +2919,10 @@ public final class NetworkCapabilities implements Parcelable { } } - if ((mCaps.getEnterpriseCapabilitySubLevels().length != 0) + if ((mCaps.getEnterpriseIds().length != 0) && !mCaps.hasCapability(NET_CAPABILITY_ENTERPRISE)) { - throw new IllegalStateException("Enterprise capability sublevel is applicable only" - + " with ENTERPRISE capability."); + throw new IllegalStateException("Enterprise capability identifier is applicable" + + " only with ENTERPRISE capability."); } return new NetworkCapabilities(mCaps); } diff --git a/framework/src/android/net/ProfileNetworkPreference.java b/framework/src/android/net/ProfileNetworkPreference.java index 0571b367d1..f43acce5bc 100644 --- a/framework/src/android/net/ProfileNetworkPreference.java +++ b/framework/src/android/net/ProfileNetworkPreference.java @@ -18,6 +18,8 @@ package android.net; import static android.annotation.SystemApi.Client.MODULE_LIBRARIES; import static android.net.ConnectivityManager.PROFILE_NETWORK_PREFERENCE_DEFAULT; +import static android.net.NetworkCapabilities.NET_ENTERPRISE_ID_1; +import static android.net.NetworkCapabilities.NET_ENTERPRISE_ID_5; import android.annotation.NonNull; import android.annotation.Nullable; @@ -38,12 +40,15 @@ import java.util.Objects; @SystemApi(client = MODULE_LIBRARIES) public final class ProfileNetworkPreference implements Parcelable { private final @ProfileNetworkPreferencePolicy int mPreference; + private final @NetworkCapabilities.EnterpriseId int mPreferenceEnterpriseId; private final List mIncludedUids; private final List mExcludedUids; private ProfileNetworkPreference(int preference, List includedUids, - List excludedUids) { + List excludedUids, + @NetworkCapabilities.EnterpriseId int preferenceEnterpriseId) { mPreference = preference; + mPreferenceEnterpriseId = preferenceEnterpriseId; if (includedUids != null) { mIncludedUids = new ArrayList<>(includedUids); } else { @@ -61,6 +66,7 @@ public final class ProfileNetworkPreference implements Parcelable { mPreference = in.readInt(); mIncludedUids = in.readArrayList(Integer.class.getClassLoader()); mExcludedUids = in.readArrayList(Integer.class.getClassLoader()); + mPreferenceEnterpriseId = in.readInt(); } public int getPreference() { @@ -95,12 +101,30 @@ public final class ProfileNetworkPreference implements Parcelable { return new ArrayList<>(mExcludedUids); } + /** + * Get preference enterprise identifier. + * + * Preference enterprise identifier will be used to create different network preferences + * within enterprise preference category. + * Valid values starts from PROFILE_NETWORK_PREFERENCE_ENTERPRISE_ID_1 to + * NetworkCapabilities.NET_ENTERPRISE_ID_5. + * Preference identifier is not applicable if preference is set as + * PROFILE_NETWORK_PREFERENCE_DEFAULT. Default value is + * NetworkCapabilities.NET_ENTERPRISE_ID_1. + * @return Preference enterprise identifier. + * + */ + public @NetworkCapabilities.EnterpriseId int getPreferenceEnterpriseId() { + return mPreferenceEnterpriseId; + } + @Override public String toString() { return "ProfileNetworkPreference{" + "mPreference=" + getPreference() + "mIncludedUids=" + mIncludedUids.toString() + "mExcludedUids=" + mExcludedUids.toString() + + "mPreferenceEnterpriseId=" + mPreferenceEnterpriseId + '}'; } @@ -111,12 +135,14 @@ public final class ProfileNetworkPreference implements Parcelable { final ProfileNetworkPreference that = (ProfileNetworkPreference) o; return mPreference == that.mPreference && (Objects.equals(mIncludedUids, that.mIncludedUids)) - && (Objects.equals(mExcludedUids, that.mExcludedUids)); + && (Objects.equals(mExcludedUids, that.mExcludedUids)) + && mPreferenceEnterpriseId == that.mPreferenceEnterpriseId; } @Override public int hashCode() { return mPreference + + mPreferenceEnterpriseId * 2 + (Objects.hashCode(mIncludedUids) * 11) + (Objects.hashCode(mExcludedUids) * 13); } @@ -130,6 +156,7 @@ public final class ProfileNetworkPreference implements Parcelable { PROFILE_NETWORK_PREFERENCE_DEFAULT; private @NonNull List mIncludedUids = new ArrayList<>(); private @NonNull List mExcludedUids = new ArrayList<>(); + private int mPreferenceEnterpriseId; /** * Constructs an empty Builder with PROFILE_NETWORK_PREFERENCE_DEFAULT profile preference @@ -191,7 +218,24 @@ public final class ProfileNetworkPreference implements Parcelable { return this; } - /** + /** + * Check if given preference enterprise identifier is valid + * + * Valid values starts from PROFILE_NETWORK_PREFERENCE_ENTERPRISE_ID_1 to + * NetworkCapabilities.NET_ENTERPRISE_ID_5. + * @return True if valid else false + * @hide + */ + private boolean isEnterpriseIdentifierValid( + @NetworkCapabilities.EnterpriseId int identifier) { + if ((identifier >= NET_ENTERPRISE_ID_1) + && (identifier <= NET_ENTERPRISE_ID_5)) { + return true; + } + return false; + } + + /** * Returns an instance of {@link ProfileNetworkPreference} created from the * fields set on this builder. */ @@ -201,7 +245,35 @@ public final class ProfileNetworkPreference implements Parcelable { throw new IllegalArgumentException("Both includedUids and excludedUids " + "cannot be nonempty"); } - return new ProfileNetworkPreference(mPreference, mIncludedUids, mExcludedUids); + + if (((mPreference != PROFILE_NETWORK_PREFERENCE_DEFAULT) + && (!isEnterpriseIdentifierValid(mPreferenceEnterpriseId))) + || ((mPreference == PROFILE_NETWORK_PREFERENCE_DEFAULT) + && (mPreferenceEnterpriseId != 0))) { + throw new IllegalStateException("Invalid preference enterprise identifier"); + } + return new ProfileNetworkPreference(mPreference, mIncludedUids, + mExcludedUids, mPreferenceEnterpriseId); + } + + /** + * Set the preference enterprise identifier. + * + * Preference enterprise identifier will be used to create different network preferences + * within enterprise preference category. + * Valid values starts from NetworkCapabilities.NET_ENTERPRISE_ID_1 to + * NetworkCapabilities.NET_ENTERPRISE_ID_5. + * Preference identifier is not applicable if preference is set as + * PROFILE_NETWORK_PREFERENCE_DEFAULT. Default value is + * NetworkCapabilities.NET_ENTERPRISE_ID_1. + * @param preferenceId preference sub level + * @return The builder to facilitate chaining. + */ + @NonNull + public Builder setPreferenceEnterpriseId( + @NetworkCapabilities.EnterpriseId int preferenceId) { + mPreferenceEnterpriseId = preferenceId; + return this; } } @@ -210,6 +282,7 @@ public final class ProfileNetworkPreference implements Parcelable { dest.writeInt(mPreference); dest.writeList(mIncludedUids); dest.writeList(mExcludedUids); + dest.writeInt(mPreferenceEnterpriseId); } @Override diff --git a/service/src/com/android/server/ConnectivityService.java b/service/src/com/android/server/ConnectivityService.java index 6c27c4a33c..ae6fe11f5a 100644 --- a/service/src/com/android/server/ConnectivityService.java +++ b/service/src/com/android/server/ConnectivityService.java @@ -73,6 +73,8 @@ import static android.net.NetworkCapabilities.NET_CAPABILITY_OEM_PAID; import static android.net.NetworkCapabilities.NET_CAPABILITY_OEM_PRIVATE; import static android.net.NetworkCapabilities.NET_CAPABILITY_PARTIAL_CONNECTIVITY; import static android.net.NetworkCapabilities.NET_CAPABILITY_VALIDATED; +import static android.net.NetworkCapabilities.NET_ENTERPRISE_ID_1; +import static android.net.NetworkCapabilities.NET_ENTERPRISE_ID_5; import static android.net.NetworkCapabilities.REDACT_FOR_ACCESS_FINE_LOCATION; import static android.net.NetworkCapabilities.REDACT_FOR_LOCAL_MAC_ADDRESS; import static android.net.NetworkCapabilities.REDACT_FOR_NETWORK_SETTINGS; @@ -10147,11 +10149,19 @@ public class ConnectivityService extends IConnectivityManager.Stub switch (preference.getPreference()) { case ConnectivityManager.PROFILE_NETWORK_PREFERENCE_DEFAULT: nc = null; + if (preference.getPreferenceEnterpriseId() != 0) { + throw new IllegalArgumentException( + "Invalid enterprise identifier in setProfileNetworkPreferences"); + } break; case ConnectivityManager.PROFILE_NETWORK_PREFERENCE_ENTERPRISE_NO_FALLBACK: allowFallback = false; // continue to process the enterprise preference. case ConnectivityManager.PROFILE_NETWORK_PREFERENCE_ENTERPRISE: + if (!isEnterpriseIdentifierValid(preference.getPreferenceEnterpriseId())) { + throw new IllegalArgumentException( + "Invalid enterprise identifier in setProfileNetworkPreferences"); + } final Set uidRangeSet = getUidListToBeAppliedForNetworkPreference(profile, preference); if (!isRangeAlreadyInPreferenceList(preferenceList, uidRangeSet)) { @@ -10161,6 +10171,8 @@ public class ConnectivityService extends IConnectivityManager.Stub "Overlapping uid range in setProfileNetworkPreferences"); } nc.addCapability(NET_CAPABILITY_ENTERPRISE); + nc.addEnterpriseId( + preference.getPreferenceEnterpriseId()); nc.removeCapability(NET_CAPABILITY_NOT_RESTRICTED); break; default: @@ -10203,6 +10215,15 @@ public class ConnectivityService extends IConnectivityManager.Stub return uidRangeSet; } + private boolean isEnterpriseIdentifierValid( + @NetworkCapabilities.EnterpriseId int identifier) { + if ((identifier >= NET_ENTERPRISE_ID_1) + && (identifier <= NET_ENTERPRISE_ID_5)) { + return true; + } + return false; + } + private void validateNetworkCapabilitiesOfProfileNetworkPreference( @Nullable final NetworkCapabilities nc) { if (null == nc) return; // Null caps are always allowed. It means to remove the setting. diff --git a/tests/common/java/android/net/NetworkCapabilitiesTest.java b/tests/common/java/android/net/NetworkCapabilitiesTest.java index 09d36e54c1..d03629d6ee 100644 --- a/tests/common/java/android/net/NetworkCapabilitiesTest.java +++ b/tests/common/java/android/net/NetworkCapabilitiesTest.java @@ -23,11 +23,6 @@ import static android.net.NetworkCapabilities.NET_CAPABILITY_CAPTIVE_PORTAL; import static android.net.NetworkCapabilities.NET_CAPABILITY_CBS; import static android.net.NetworkCapabilities.NET_CAPABILITY_EIMS; import static android.net.NetworkCapabilities.NET_CAPABILITY_ENTERPRISE; -import static android.net.NetworkCapabilities.NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_1; -import static android.net.NetworkCapabilities.NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_2; -import static android.net.NetworkCapabilities.NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_3; -import static android.net.NetworkCapabilities.NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_4; -import static android.net.NetworkCapabilities.NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_5; import static android.net.NetworkCapabilities.NET_CAPABILITY_FOREGROUND; import static android.net.NetworkCapabilities.NET_CAPABILITY_INTERNET; import static android.net.NetworkCapabilities.NET_CAPABILITY_MMS; @@ -42,6 +37,11 @@ import static android.net.NetworkCapabilities.NET_CAPABILITY_PARTIAL_CONNECTIVIT import static android.net.NetworkCapabilities.NET_CAPABILITY_TRUSTED; import static android.net.NetworkCapabilities.NET_CAPABILITY_VALIDATED; import static android.net.NetworkCapabilities.NET_CAPABILITY_WIFI_P2P; +import static android.net.NetworkCapabilities.NET_ENTERPRISE_ID_1; +import static android.net.NetworkCapabilities.NET_ENTERPRISE_ID_2; +import static android.net.NetworkCapabilities.NET_ENTERPRISE_ID_3; +import static android.net.NetworkCapabilities.NET_ENTERPRISE_ID_4; +import static android.net.NetworkCapabilities.NET_ENTERPRISE_ID_5; import static android.net.NetworkCapabilities.REDACT_FOR_ACCESS_FINE_LOCATION; import static android.net.NetworkCapabilities.REDACT_FOR_LOCAL_MAC_ADDRESS; import static android.net.NetworkCapabilities.REDACT_FOR_NETWORK_SETTINGS; @@ -790,78 +790,79 @@ public class NetworkCapabilitiesTest { } @Test @IgnoreUpTo(SC_V2) // TODO: Use to Build.VERSION_CODES.SC_V2 when available - public void testEnterpriseCapabilitySubLevel() { + public void testEnterpriseId() { final NetworkCapabilities nc1 = new NetworkCapabilities.Builder() .addCapability(NET_CAPABILITY_ENTERPRISE) - .addEnterpriseCapabilitySubLevel(NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_1) + .addEnterpriseId(NET_ENTERPRISE_ID_1) .build(); - assertEquals(1, nc1.getEnterpriseCapabilitySubLevels().length); - assertEquals(NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_1, - nc1.getEnterpriseCapabilitySubLevels()[0]); + assertEquals(1, nc1.getEnterpriseIds().length); + assertEquals(NET_ENTERPRISE_ID_1, + nc1.getEnterpriseIds()[0]); final NetworkCapabilities nc2 = new NetworkCapabilities.Builder() .addCapability(NET_CAPABILITY_ENTERPRISE) - .addEnterpriseCapabilitySubLevel(NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_1) - .addEnterpriseCapabilitySubLevel(NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_2) + .addEnterpriseId(NET_ENTERPRISE_ID_1) + .addEnterpriseId(NET_ENTERPRISE_ID_2) .build(); - assertEquals(2, nc2.getEnterpriseCapabilitySubLevels().length); - assertEquals(NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_1, - nc2.getEnterpriseCapabilitySubLevels()[0]); - assertEquals(NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_2, - nc2.getEnterpriseCapabilitySubLevels()[1]); + assertEquals(2, nc2.getEnterpriseIds().length); + assertEquals(NET_ENTERPRISE_ID_1, + nc2.getEnterpriseIds()[0]); + assertEquals(NET_ENTERPRISE_ID_2, + nc2.getEnterpriseIds()[1]); final NetworkCapabilities nc3 = new NetworkCapabilities.Builder() .addCapability(NET_CAPABILITY_ENTERPRISE) - .addEnterpriseCapabilitySubLevel(NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_1) - .addEnterpriseCapabilitySubLevel(NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_2) - .addEnterpriseCapabilitySubLevel(NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_3) - .addEnterpriseCapabilitySubLevel(NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_4) - .addEnterpriseCapabilitySubLevel(NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_5) + .addEnterpriseId(NET_ENTERPRISE_ID_1) + .addEnterpriseId(NET_ENTERPRISE_ID_2) + .addEnterpriseId(NET_ENTERPRISE_ID_3) + .addEnterpriseId(NET_ENTERPRISE_ID_4) + .addEnterpriseId(NET_ENTERPRISE_ID_5) .build(); - assertEquals(5, nc3.getEnterpriseCapabilitySubLevels().length); - assertEquals(NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_1, - nc3.getEnterpriseCapabilitySubLevels()[0]); - assertEquals(NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_2, - nc3.getEnterpriseCapabilitySubLevels()[1]); - assertEquals(NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_3, - nc3.getEnterpriseCapabilitySubLevels()[2]); - assertEquals(NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_4, - nc3.getEnterpriseCapabilitySubLevels()[3]); - assertEquals(NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_5, - nc3.getEnterpriseCapabilitySubLevels()[4]); + assertEquals(5, nc3.getEnterpriseIds().length); + assertEquals(NET_ENTERPRISE_ID_1, + nc3.getEnterpriseIds()[0]); + assertEquals(NET_ENTERPRISE_ID_2, + nc3.getEnterpriseIds()[1]); + assertEquals(NET_ENTERPRISE_ID_3, + nc3.getEnterpriseIds()[2]); + assertEquals(NET_ENTERPRISE_ID_4, + nc3.getEnterpriseIds()[3]); + assertEquals(NET_ENTERPRISE_ID_5, + nc3.getEnterpriseIds()[4]); final Class illegalArgumentExceptionClass = IllegalArgumentException.class; assertThrows(illegalArgumentExceptionClass, () -> new NetworkCapabilities.Builder() - .addEnterpriseCapabilitySubLevel(6) + .addEnterpriseId(6) .build()); assertThrows(illegalArgumentExceptionClass, () -> new NetworkCapabilities.Builder() - .removeEnterpriseCapabilitySubLevel(6) + .removeEnterpriseId(6) .build()); final Class illegalStateException = IllegalStateException.class; assertThrows(illegalStateException, () -> new NetworkCapabilities.Builder() - .addEnterpriseCapabilitySubLevel(NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_1) + .addEnterpriseId(NET_ENTERPRISE_ID_1) .build()); final NetworkCapabilities nc4 = new NetworkCapabilities.Builder() .addCapability(NET_CAPABILITY_ENTERPRISE) - .addEnterpriseCapabilitySubLevel(NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_1) - .addEnterpriseCapabilitySubLevel(NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_2) - .removeEnterpriseCapabilitySubLevel(NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_1) - .removeEnterpriseCapabilitySubLevel(NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_2) + .addEnterpriseId(NET_ENTERPRISE_ID_1) + .addEnterpriseId(NET_ENTERPRISE_ID_2) + .removeEnterpriseId(NET_ENTERPRISE_ID_1) + .removeEnterpriseId(NET_ENTERPRISE_ID_2) .build(); - assertEquals(0, nc4.getEnterpriseCapabilitySubLevels().length); + assertEquals(1, nc4.getEnterpriseIds().length); + assertTrue(nc4.hasEnterpriseId(NET_ENTERPRISE_ID_1)); final NetworkCapabilities nc5 = new NetworkCapabilities.Builder() .addCapability(NET_CAPABILITY_CBS) - .addEnterpriseCapabilitySubLevel(NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_1) - .addEnterpriseCapabilitySubLevel(NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_2) - .removeEnterpriseCapabilitySubLevel(NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_1) - .removeEnterpriseCapabilitySubLevel(NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_2) + .addEnterpriseId(NET_ENTERPRISE_ID_1) + .addEnterpriseId(NET_ENTERPRISE_ID_2) + .removeEnterpriseId(NET_ENTERPRISE_ID_1) + .removeEnterpriseId(NET_ENTERPRISE_ID_2) .build(); assertTrue(nc4.satisfiedByNetworkCapabilities(nc1)); - assertFalse(nc1.satisfiedByNetworkCapabilities(nc4)); + assertTrue(nc1.satisfiedByNetworkCapabilities(nc4)); assertFalse(nc3.satisfiedByNetworkCapabilities(nc2)); assertTrue(nc2.satisfiedByNetworkCapabilities(nc3)); diff --git a/tests/unit/java/com/android/server/ConnectivityServiceTest.java b/tests/unit/java/com/android/server/ConnectivityServiceTest.java index 14ff981b4f..652aee9084 100644 --- a/tests/unit/java/com/android/server/ConnectivityServiceTest.java +++ b/tests/unit/java/com/android/server/ConnectivityServiceTest.java @@ -101,6 +101,7 @@ import static android.net.NetworkCapabilities.NET_CAPABILITY_VALIDATED; import static android.net.NetworkCapabilities.NET_CAPABILITY_VSIM; import static android.net.NetworkCapabilities.NET_CAPABILITY_WIFI_P2P; import static android.net.NetworkCapabilities.NET_CAPABILITY_XCAP; +import static android.net.NetworkCapabilities.NET_ENTERPRISE_ID_1; import static android.net.NetworkCapabilities.REDACT_FOR_ACCESS_FINE_LOCATION; import static android.net.NetworkCapabilities.REDACT_FOR_LOCAL_MAC_ADDRESS; import static android.net.NetworkCapabilities.REDACT_FOR_NETWORK_SETTINGS; @@ -13780,6 +13781,14 @@ public class ConnectivityServiceTest { return new TestNetworkAgentWrapper(TRANSPORT_CELLULAR, new LinkProperties(), workNc); } + private TestNetworkAgentWrapper makeEnterpriseNetworkAgent(int enterpriseId) throws Exception { + final NetworkCapabilities workNc = new NetworkCapabilities(); + workNc.addCapability(NET_CAPABILITY_ENTERPRISE); + workNc.removeCapability(NET_CAPABILITY_NOT_RESTRICTED); + workNc.addEnterpriseId(enterpriseId); + return new TestNetworkAgentWrapper(TRANSPORT_CELLULAR, new LinkProperties(), workNc); + } + private TestNetworkCallback mEnterpriseCallback; private UserHandle setupEnterpriseNetwork() { final UserHandle userHandle = UserHandle.of(TEST_WORK_PROFILE_USER_ID); @@ -13827,7 +13836,8 @@ public class ConnectivityServiceTest { inOrder.verify(mMockNetd).networkCreate(nativeNetworkConfigPhysical( mCellNetworkAgent.getNetwork().netId, INetd.PERMISSION_NONE)); - final TestNetworkAgentWrapper workAgent = makeEnterpriseNetworkAgent(); + final TestNetworkAgentWrapper workAgent = + makeEnterpriseNetworkAgent(profileNetworkPreference.getPreferenceEnterpriseId()); if (connectWorkProfileAgentAhead) { workAgent.connect(false); } @@ -13841,7 +13851,7 @@ public class ConnectivityServiceTest { == PROFILE_NETWORK_PREFERENCE_ENTERPRISE_NO_FALLBACK) { allowFallback = false; } - if (allowFallback) { + if (allowFallback && !connectWorkProfileAgentAhead) { // Setting a network preference for this user will create a new set of routing rules for // the UID range that corresponds to this user, inorder to define the default network // for these apps separately. This is true because the multi-layer request relevant to @@ -13858,7 +13868,7 @@ public class ConnectivityServiceTest { // The enterprise network is not ready yet. assertNoCallbacks(mSystemDefaultNetworkCallback, mDefaultNetworkCallback); - if (allowFallback) { + if (allowFallback && !connectWorkProfileAgentAhead) { assertNoCallbacks(profileDefaultNetworkCallback); } else if (!connectWorkProfileAgentAhead) { profileDefaultNetworkCallback.expectCallback(CallbackEntry.LOST, mCellNetworkAgent); @@ -13884,7 +13894,7 @@ public class ConnectivityServiceTest { uidRangeFor(testHandle, profileNetworkPreference), PREFERENCE_ORDER_PROFILE)); - if (allowFallback) { + if (allowFallback && !connectWorkProfileAgentAhead) { inOrder.verify(mMockNetd).networkRemoveUidRangesParcel(new NativeUidRangeConfig( mCellNetworkAgent.getNetwork().netId, uidRangeFor(testHandle, profileNetworkPreference), @@ -13897,7 +13907,10 @@ public class ConnectivityServiceTest { workAgent.mNetworkMonitor.forceReevaluation(Process.myUid()); profileDefaultNetworkCallback.expectCapabilitiesThat(workAgent, nc -> nc.hasCapability(NET_CAPABILITY_VALIDATED) - && nc.hasCapability(NET_CAPABILITY_ENTERPRISE)); + && nc.hasCapability(NET_CAPABILITY_ENTERPRISE) + && nc.hasEnterpriseId( + profileNetworkPreference.getPreferenceEnterpriseId()) + && nc.getEnterpriseIds().length == 1); if (disAllowProfileDefaultNetworkCallback != null) { assertNoCallbacks(disAllowProfileDefaultNetworkCallback); } @@ -13988,7 +14001,8 @@ public class ConnectivityServiceTest { // If the control comes here, callbacks seem to behave correctly in the presence of // a default network when the enterprise network goes up and down. Now, make sure they // also behave correctly in the absence of a system-wide default network. - final TestNetworkAgentWrapper workAgent2 = makeEnterpriseNetworkAgent(); + final TestNetworkAgentWrapper workAgent2 = + makeEnterpriseNetworkAgent(profileNetworkPreference.getPreferenceEnterpriseId()); workAgent2.connect(false); profileDefaultNetworkCallback.expectAvailableCallbacksUnvalidated(workAgent2); @@ -14006,7 +14020,10 @@ public class ConnectivityServiceTest { workAgent2.mNetworkMonitor.forceReevaluation(Process.myUid()); profileDefaultNetworkCallback.expectCapabilitiesThat(workAgent2, nc -> nc.hasCapability(NET_CAPABILITY_ENTERPRISE) - && !nc.hasCapability(NET_CAPABILITY_NOT_RESTRICTED)); + && !nc.hasCapability(NET_CAPABILITY_NOT_RESTRICTED) + && nc.hasEnterpriseId( + profileNetworkPreference.getPreferenceEnterpriseId()) + && nc.getEnterpriseIds().length == 1); if (disAllowProfileDefaultNetworkCallback != null) { assertNoCallbacks(disAllowProfileDefaultNetworkCallback); } @@ -14037,10 +14054,11 @@ public class ConnectivityServiceTest { @Test public void testPreferenceForUserNetworkUpDown() throws Exception { final UserHandle testHandle = setupEnterpriseNetwork(); + registerDefaultNetworkCallbacks(); ProfileNetworkPreference.Builder profileNetworkPreferenceBuilder = new ProfileNetworkPreference.Builder(); profileNetworkPreferenceBuilder.setPreference(PROFILE_NETWORK_PREFERENCE_ENTERPRISE); - registerDefaultNetworkCallbacks(); + profileNetworkPreferenceBuilder.setPreferenceEnterpriseId(NET_ENTERPRISE_ID_1); testPreferenceForUserNetworkUpDownForGivenPreference( profileNetworkPreferenceBuilder.build(), false, testHandle, mProfileDefaultNetworkCallback, null); @@ -14058,6 +14076,7 @@ public class ConnectivityServiceTest { new ProfileNetworkPreference.Builder(); profileNetworkPreferenceBuilder.setPreference( PROFILE_NETWORK_PREFERENCE_ENTERPRISE_NO_FALLBACK); + profileNetworkPreferenceBuilder.setPreferenceEnterpriseId(NET_ENTERPRISE_ID_1); registerDefaultNetworkCallbacks(); testPreferenceForUserNetworkUpDownForGivenPreference( profileNetworkPreferenceBuilder.build(), false, @@ -14078,6 +14097,7 @@ public class ConnectivityServiceTest { new ProfileNetworkPreference.Builder(); profileNetworkPreferenceBuilder.setPreference( PROFILE_NETWORK_PREFERENCE_ENTERPRISE_NO_FALLBACK); + profileNetworkPreferenceBuilder.setPreferenceEnterpriseId(NET_ENTERPRISE_ID_1); registerDefaultNetworkCallbacks(); testPreferenceForUserNetworkUpDownForGivenPreference( profileNetworkPreferenceBuilder.build(), true, testHandle, @@ -14094,6 +14114,7 @@ public class ConnectivityServiceTest { ProfileNetworkPreference.Builder profileNetworkPreferenceBuilder = new ProfileNetworkPreference.Builder(); profileNetworkPreferenceBuilder.setPreference(PROFILE_NETWORK_PREFERENCE_ENTERPRISE); + profileNetworkPreferenceBuilder.setPreferenceEnterpriseId(NET_ENTERPRISE_ID_1); profileNetworkPreferenceBuilder.setIncludedUids( List.of(testHandle.getUid(TEST_WORK_PROFILE_APP_UID))); registerDefaultNetworkCallbacks(); @@ -14112,6 +14133,7 @@ public class ConnectivityServiceTest { ProfileNetworkPreference.Builder profileNetworkPreferenceBuilder = new ProfileNetworkPreference.Builder(); profileNetworkPreferenceBuilder.setPreference(PROFILE_NETWORK_PREFERENCE_ENTERPRISE); + profileNetworkPreferenceBuilder.setPreferenceEnterpriseId(NET_ENTERPRISE_ID_1); profileNetworkPreferenceBuilder.setIncludedUids( List.of(testHandle.getUid(TEST_WORK_PROFILE_APP_UID_2))); registerDefaultNetworkCallbacks(); @@ -14130,6 +14152,7 @@ public class ConnectivityServiceTest { ProfileNetworkPreference.Builder profileNetworkPreferenceBuilder = new ProfileNetworkPreference.Builder(); profileNetworkPreferenceBuilder.setPreference(PROFILE_NETWORK_PREFERENCE_ENTERPRISE); + profileNetworkPreferenceBuilder.setPreferenceEnterpriseId(NET_ENTERPRISE_ID_1); profileNetworkPreferenceBuilder.setExcludedUids( List.of(testHandle.getUid(TEST_WORK_PROFILE_APP_UID_2))); registerDefaultNetworkCallbacks(); @@ -14149,6 +14172,7 @@ public class ConnectivityServiceTest { ProfileNetworkPreference.Builder profileNetworkPreferenceBuilder = new ProfileNetworkPreference.Builder(); profileNetworkPreferenceBuilder.setPreference(PROFILE_NETWORK_PREFERENCE_ENTERPRISE); + profileNetworkPreferenceBuilder.setPreferenceEnterpriseId(NET_ENTERPRISE_ID_1); profileNetworkPreferenceBuilder.setExcludedUids( List.of(testHandle.getUid(0) - 1)); final TestOnCompleteListener listener = new TestOnCompleteListener(); @@ -14178,6 +14202,7 @@ public class ConnectivityServiceTest { ProfileNetworkPreference.Builder profileNetworkPreferenceBuilder2 = new ProfileNetworkPreference.Builder(); profileNetworkPreferenceBuilder2.setPreference(PROFILE_NETWORK_PREFERENCE_ENTERPRISE); + profileNetworkPreferenceBuilder2.setPreferenceEnterpriseId(NET_ENTERPRISE_ID_1); profileNetworkPreferenceBuilder2.setIncludedUids( List.of(testHandle.getUid(TEST_WORK_PROFILE_APP_UID_2))); profileNetworkPreferenceBuilder.setIncludedUids( @@ -14212,6 +14237,84 @@ public class ConnectivityServiceTest { r -> r.run(), listener)); } + /** + * Make sure per-profile networking preference behaves as expected when the enterprise network + * goes up and down while the preference is active. Make sure they behave as expected whether + * there is a general default network or not when configured to fallback to default network + * along with already connected enterprise work agent + */ + @Test + public void testPreferenceForUserNetworkUpDownWithFallbackWithAlreadyConnectedWorkAgent() + throws Exception { + final UserHandle testHandle = setupEnterpriseNetwork(); + ProfileNetworkPreference.Builder profileNetworkPreferenceBuilder = + new ProfileNetworkPreference.Builder(); + profileNetworkPreferenceBuilder.setPreference(PROFILE_NETWORK_PREFERENCE_ENTERPRISE); + profileNetworkPreferenceBuilder.setPreferenceEnterpriseId(NET_ENTERPRISE_ID_1); + registerDefaultNetworkCallbacks(); + testPreferenceForUserNetworkUpDownForGivenPreference( + profileNetworkPreferenceBuilder.build(), true, + testHandle, mProfileDefaultNetworkCallback, + null); + } + + /** + * Make sure per-profile networking preference behaves as expected when the enterprise network + * goes up and down while the preference is active for a given enterprise identifier + */ + @Test + public void testPreferenceForUserNetworkUpDownWithDefaultEnterpriseId() + throws Exception { + final UserHandle testHandle = setupEnterpriseNetwork(); + ProfileNetworkPreference.Builder profileNetworkPreferenceBuilder = + new ProfileNetworkPreference.Builder(); + profileNetworkPreferenceBuilder.setPreference( + PROFILE_NETWORK_PREFERENCE_ENTERPRISE_NO_FALLBACK); + profileNetworkPreferenceBuilder.setPreferenceEnterpriseId(NET_ENTERPRISE_ID_1); + registerDefaultNetworkCallbacks(); + testPreferenceForUserNetworkUpDownForGivenPreference( + profileNetworkPreferenceBuilder.build(), true, + testHandle, mProfileDefaultNetworkCallback, + null); + } + + /** + * Make sure per-profile networking preference behaves as expected when the enterprise network + * goes up and down while the preference is active for a given enterprise identifier + */ + @Test + public void testPreferenceForUserNetworkUpDownWithId2() + throws Exception { + final UserHandle testHandle = setupEnterpriseNetwork(); + ProfileNetworkPreference.Builder profileNetworkPreferenceBuilder = + new ProfileNetworkPreference.Builder(); + profileNetworkPreferenceBuilder.setPreference( + PROFILE_NETWORK_PREFERENCE_ENTERPRISE_NO_FALLBACK); + profileNetworkPreferenceBuilder.setPreferenceEnterpriseId( + NetworkCapabilities.NET_ENTERPRISE_ID_2); + registerDefaultNetworkCallbacks(); + testPreferenceForUserNetworkUpDownForGivenPreference( + profileNetworkPreferenceBuilder.build(), true, + testHandle, mProfileDefaultNetworkCallback, null); + } + + /** + * Make sure per-profile networking preference behaves as expected when the enterprise network + * goes up and down while the preference is active for a given enterprise identifier + */ + @Test + public void testPreferenceForUserNetworkUpDownWithInvalidId() + throws Exception { + ProfileNetworkPreference.Builder profileNetworkPreferenceBuilder = + new ProfileNetworkPreference.Builder(); + profileNetworkPreferenceBuilder.setPreference( + PROFILE_NETWORK_PREFERENCE_ENTERPRISE_NO_FALLBACK); + profileNetworkPreferenceBuilder.setPreferenceEnterpriseId(0); + registerDefaultNetworkCallbacks(); + assertThrows("Should not be able to set invalid enterprise id", + IllegalStateException.class, () -> profileNetworkPreferenceBuilder.build()); + } + /** * Test that, in a given networking context, calling setPreferenceForUser to set per-profile * defaults on then off works as expected. @@ -14362,10 +14465,15 @@ public class ConnectivityServiceTest { public void testProfileNetworkPrefWrongPreference() throws Exception { final UserHandle testHandle = UserHandle.of(TEST_WORK_PROFILE_USER_ID); mServiceContext.setWorkProfile(testHandle, true); + ProfileNetworkPreference.Builder profileNetworkPreferenceBuilder = + new ProfileNetworkPreference.Builder(); + profileNetworkPreferenceBuilder.setPreference( + PROFILE_NETWORK_PREFERENCE_ENTERPRISE_NO_FALLBACK + 1); + profileNetworkPreferenceBuilder.setPreferenceEnterpriseId(NET_ENTERPRISE_ID_1); assertThrows("Should not be able to set an illegal preference", IllegalArgumentException.class, - () -> mCm.setProfileNetworkPreference(testHandle, - PROFILE_NETWORK_PREFERENCE_ENTERPRISE_NO_FALLBACK + 1, + () -> mCm.setProfileNetworkPreferences(testHandle, + List.of(profileNetworkPreferenceBuilder.build()), null, null)); }