diff options
| author | Sooraj Sasindran <sasindran@google.com> | 2021-11-29 12:40:09 -0800 |
|---|---|---|
| committer | Sooraj Sasindran <sasindran@google.com> | 2022-01-21 01:05:12 -0800 |
| commit | 499117f2790c4eb2a1decb0ae12fd82a805ca9a0 (patch) | |
| tree | f27dd1d28eecf046fe0f1fd2a30678da66dffc8e /framework/src/android/net/ProfileNetworkPreference.java | |
| parent | 38276f1b9ce9d02ea71aac5f9c423dc5ac2dfc80 (diff) | |
Ability to specify which all applications fall under enterprise slice.
Bug: 194332512
Test: unit test
Change-Id: I94549a41aaa717add22b0a3e5035beacf6f1b8f2
Diffstat (limited to 'framework/src/android/net/ProfileNetworkPreference.java')
| -rw-r--r-- | framework/src/android/net/ProfileNetworkPreference.java | 113 |
1 files changed, 109 insertions, 4 deletions
diff --git a/framework/src/android/net/ProfileNetworkPreference.java b/framework/src/android/net/ProfileNetworkPreference.java index 2ce1698cf0..0571b367d1 100644 --- a/framework/src/android/net/ProfileNetworkPreference.java +++ b/framework/src/android/net/ProfileNetworkPreference.java @@ -20,11 +20,16 @@ import static android.annotation.SystemApi.Client.MODULE_LIBRARIES; import static android.net.ConnectivityManager.PROFILE_NETWORK_PREFERENCE_DEFAULT; import android.annotation.NonNull; +import android.annotation.Nullable; import android.annotation.SystemApi; import android.net.ConnectivityManager.ProfileNetworkPreferencePolicy; import android.os.Parcel; import android.os.Parcelable; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + /** * Network preferences to be set for the user profile * {@link ProfileNetworkPreferencePolicy}. @@ -33,23 +38,69 @@ import android.os.Parcelable; @SystemApi(client = MODULE_LIBRARIES) public final class ProfileNetworkPreference implements Parcelable { private final @ProfileNetworkPreferencePolicy int mPreference; + private final List<Integer> mIncludedUids; + private final List<Integer> mExcludedUids; - private ProfileNetworkPreference(int preference) { + private ProfileNetworkPreference(int preference, List<Integer> includedUids, + List<Integer> excludedUids) { mPreference = preference; + if (includedUids != null) { + mIncludedUids = new ArrayList<>(includedUids); + } else { + mIncludedUids = new ArrayList<>(); + } + + if (excludedUids != null) { + mExcludedUids = new ArrayList<>(excludedUids); + } else { + mExcludedUids = new ArrayList<>(); + } } private ProfileNetworkPreference(Parcel in) { mPreference = in.readInt(); + mIncludedUids = in.readArrayList(Integer.class.getClassLoader()); + mExcludedUids = in.readArrayList(Integer.class.getClassLoader()); } public int getPreference() { return mPreference; } + /** + * Get the list of UIDs subject to this preference. + * + * Included UIDs and Excluded UIDs can't both be non-empty. + * if both are empty, it means this request applies to all uids in the user profile. + * if included is not empty, then only included UIDs are applied. + * if excluded is not empty, then it is all uids in the user profile except these UIDs. + * @return List of uids included for the profile preference. + * {@see #getExcludedUids()} + */ + public @NonNull List<Integer> getIncludedUids() { + return new ArrayList<>(mIncludedUids); + } + + /** + * Get the list of UIDS excluded from this preference. + * + * <ul>Included UIDs and Excluded UIDs can't both be non-empty.</ul> + * <ul>If both are empty, it means this request applies to all uids in the user profile.</ul> + * <ul>If included is not empty, then only included UIDs are applied.</ul> + * <ul>If excluded is not empty, then it is all uids in the user profile except these UIDs.</ul> + * @return List of uids not included for the profile preference. + * {@see #getIncludedUids()} + */ + public @NonNull List<Integer> getExcludedUids() { + return new ArrayList<>(mExcludedUids); + } + @Override public String toString() { return "ProfileNetworkPreference{" + "mPreference=" + getPreference() + + "mIncludedUids=" + mIncludedUids.toString() + + "mExcludedUids=" + mExcludedUids.toString() + '}'; } @@ -58,12 +109,16 @@ public final class ProfileNetworkPreference implements Parcelable { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; final ProfileNetworkPreference that = (ProfileNetworkPreference) o; - return mPreference == that.mPreference; + return mPreference == that.mPreference + && (Objects.equals(mIncludedUids, that.mIncludedUids)) + && (Objects.equals(mExcludedUids, that.mExcludedUids)); } @Override public int hashCode() { - return mPreference; + return mPreference + + (Objects.hashCode(mIncludedUids) * 11) + + (Objects.hashCode(mExcludedUids) * 13); } /** @@ -73,6 +128,8 @@ public final class ProfileNetworkPreference implements Parcelable { public static final class Builder { private @ProfileNetworkPreferencePolicy int mPreference = PROFILE_NETWORK_PREFERENCE_DEFAULT; + private @NonNull List<Integer> mIncludedUids = new ArrayList<>(); + private @NonNull List<Integer> mExcludedUids = new ArrayList<>(); /** * Constructs an empty Builder with PROFILE_NETWORK_PREFERENCE_DEFAULT profile preference @@ -93,18 +150,66 @@ public final class ProfileNetworkPreference implements Parcelable { } /** + * This is a list of uids for which profile perefence is set. + * Null would mean that this preference applies to all uids in the profile. + * {@see #setExcludedUids(List<Integer>)} + * Included UIDs and Excluded UIDs can't both be non-empty. + * if both are empty, it means this request applies to all uids in the user profile. + * if included is not empty, then only included UIDs are applied. + * if excluded is not empty, then it is all uids in the user profile except these UIDs. + * @param uids list of uids that are included + * @return The builder to facilitate chaining. + */ + @NonNull + public Builder setIncludedUids(@Nullable List<Integer> uids) { + if (uids != null) { + mIncludedUids = new ArrayList<Integer>(uids); + } else { + mIncludedUids = new ArrayList<Integer>(); + } + return this; + } + + + /** + * This is a list of uids that are excluded for the profile perefence. + * {@see #setIncludedUids(List<Integer>)} + * Included UIDs and Excluded UIDs can't both be non-empty. + * if both are empty, it means this request applies to all uids in the user profile. + * if included is not empty, then only included UIDs are applied. + * if excluded is not empty, then it is all uids in the user profile except these UIDs. + * @param uids list of uids that are not included + * @return The builder to facilitate chaining. + */ + @NonNull + public Builder setExcludedUids(@Nullable List<Integer> uids) { + if (uids != null) { + mExcludedUids = new ArrayList<Integer>(uids); + } else { + mExcludedUids = new ArrayList<Integer>(); + } + return this; + } + + /** * Returns an instance of {@link ProfileNetworkPreference} created from the * fields set on this builder. */ @NonNull public ProfileNetworkPreference build() { - return new ProfileNetworkPreference(mPreference); + if (mIncludedUids.size() > 0 && mExcludedUids.size() > 0) { + throw new IllegalArgumentException("Both includedUids and excludedUids " + + "cannot be nonempty"); + } + return new ProfileNetworkPreference(mPreference, mIncludedUids, mExcludedUids); } } @Override public void writeToParcel(@NonNull android.os.Parcel dest, int flags) { dest.writeInt(mPreference); + dest.writeList(mIncludedUids); + dest.writeList(mExcludedUids); } @Override |
