Add a method to apply a set of network preferences to a user profile

Add a method to apply a set of network preferences to a user profile

Bug: 194332512
CTS-Coverage-Bug: 211133973
Test: CTS
Change-Id: I97730ea14e7c96922236fd77a591e5acadd875ba
This commit is contained in:
Sooraj Sasindran
2021-11-24 20:26:55 -08:00
parent da13f899e3
commit e7aee2736e
7 changed files with 263 additions and 43 deletions

View File

@@ -1101,7 +1101,7 @@ public class ConnectivityManager {
PROFILE_NETWORK_PREFERENCE_DEFAULT,
PROFILE_NETWORK_PREFERENCE_ENTERPRISE
})
public @interface ProfileNetworkPreference {
public @interface ProfileNetworkPreferencePolicy {
}
/**
@@ -5461,6 +5461,8 @@ public class ConnectivityManager {
* @param listener an optional listener to listen for completion of the operation.
* @throws IllegalArgumentException if {@code profile} is not a valid user profile.
* @throws SecurityException if missing the appropriate permissions.
* @deprecated Use {@link #setProfileNetworkPreferences(UserHandle, List, Executor, Runnable)}
* instead as it provides a more flexible API with more options.
* @hide
*/
// This function is for establishing per-profile default networking and can only be called by
@@ -5470,8 +5472,45 @@ public class ConnectivityManager {
@SuppressLint({"UserHandle"})
@SystemApi(client = MODULE_LIBRARIES)
@RequiresPermission(android.Manifest.permission.NETWORK_STACK)
@Deprecated
public void setProfileNetworkPreference(@NonNull final UserHandle profile,
@ProfileNetworkPreference final int preference,
@ProfileNetworkPreferencePolicy final int preference,
@Nullable @CallbackExecutor final Executor executor,
@Nullable final Runnable listener) {
ProfileNetworkPreference.Builder preferenceBuilder =
new ProfileNetworkPreference.Builder();
preferenceBuilder.setPreference(preference);
setProfileNetworkPreferences(profile,
List.of(preferenceBuilder.build()), executor, listener);
}
/**
* Set a list of default network selection policies for a user profile.
*
* Calling this API with a user handle defines the entire policy for that user handle.
* It will overwrite any setting previously set for the same user profile,
* and not affect previously set settings for other handles.
*
* Call this API with an empty list to remove settings for this user profile.
*
* See {@link ProfileNetworkPreference} for more details on each preference
* parameter.
*
* @param profile the user profile for which the preference is being set.
* @param profileNetworkPreferences the list of profile network preferences for the
* provided profile.
* @param executor an executor to execute the listener on. Optional if listener is null.
* @param listener an optional listener to listen for completion of the operation.
* @throws IllegalArgumentException if {@code profile} is not a valid user profile.
* @throws SecurityException if missing the appropriate permissions.
* @hide
*/
@SystemApi(client = MODULE_LIBRARIES)
@RequiresPermission(android.Manifest.permission.NETWORK_STACK)
public void setProfileNetworkPreferences(
@NonNull final UserHandle profile,
@NonNull List<ProfileNetworkPreference> profileNetworkPreferences,
@Nullable @CallbackExecutor final Executor executor,
@Nullable final Runnable listener) {
if (null != listener) {
@@ -5489,7 +5528,7 @@ public class ConnectivityManager {
};
}
try {
mService.setProfileNetworkPreference(profile, preference, proxy);
mService.setProfileNetworkPreferences(profile, profileNetworkPreferences, proxy);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}

View File

@@ -36,6 +36,7 @@ import android.net.NetworkScore;
import android.net.NetworkState;
import android.net.NetworkStateSnapshot;
import android.net.OemNetworkPreferences;
import android.net.ProfileNetworkPreference;
import android.net.ProxyInfo;
import android.net.UidRange;
import android.net.QosSocketInfo;
@@ -218,7 +219,8 @@ interface IConnectivityManager
void setOemNetworkPreference(in OemNetworkPreferences preference,
in IOnCompleteListener listener);
void setProfileNetworkPreference(in UserHandle profile, int preference,
void setProfileNetworkPreferences(in UserHandle profile,
in List<ProfileNetworkPreference> preferences,
in IOnCompleteListener listener);
int getRestrictBackgroundStatusByCaller();

View File

@@ -0,0 +1,128 @@
/*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.net;
import static android.annotation.SystemApi.Client.MODULE_LIBRARIES;
import static android.net.ConnectivityManager.PROFILE_NETWORK_PREFERENCE_DEFAULT;
import android.annotation.NonNull;
import android.annotation.SystemApi;
import android.net.ConnectivityManager.ProfileNetworkPreferencePolicy;
import android.os.Parcel;
import android.os.Parcelable;
/**
* Network preferences to be set for the user profile
* {@link ProfileNetworkPreferencePolicy}.
* @hide
*/
@SystemApi(client = MODULE_LIBRARIES)
public final class ProfileNetworkPreference implements Parcelable {
private final @ProfileNetworkPreferencePolicy int mPreference;
private ProfileNetworkPreference(int preference) {
mPreference = preference;
}
private ProfileNetworkPreference(Parcel in) {
mPreference = in.readInt();
}
public int getPreference() {
return mPreference;
}
@Override
public String toString() {
return "ProfileNetworkPreference{"
+ "mPreference=" + getPreference()
+ '}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
final ProfileNetworkPreference that = (ProfileNetworkPreference) o;
return mPreference == that.mPreference;
}
@Override
public int hashCode() {
return (mPreference);
}
/**
* Builder used to create {@link ProfileNetworkPreference} objects.
* Specify the preferred Network preference
*/
public static final class Builder {
private @ProfileNetworkPreferencePolicy int mPreference =
PROFILE_NETWORK_PREFERENCE_DEFAULT;
/**
* Constructs an empty Builder with PROFILE_NETWORK_PREFERENCE_DEFAULT profile preference
*/
public Builder() {}
/**
* Set the profile network preference
* See the documentation for the individual preferences for a description of the supported
* behaviors. Default value is PROFILE_NETWORK_PREFERENCE_DEFAULT.
* @param preference the desired network preference to use
* @return The builder to facilitate chaining.
*/
@NonNull
public Builder setPreference(@ProfileNetworkPreferencePolicy int preference) {
mPreference = preference;
return this;
}
/**
* Returns an instance of {@link ProfileNetworkPreference} created from the
* fields set on this builder.
*/
@NonNull
public ProfileNetworkPreference build() {
return new ProfileNetworkPreference(mPreference);
}
}
@Override
public void writeToParcel(@NonNull android.os.Parcel dest, int flags) {
dest.writeInt(mPreference);
}
@Override
public int describeContents() {
return 0;
}
@NonNull
public static final Creator<ProfileNetworkPreference> CREATOR =
new Creator<ProfileNetworkPreference>() {
@Override
public ProfileNetworkPreference[] newArray(int size) {
return new ProfileNetworkPreference[size];
}
@Override
public ProfileNetworkPreference createFromParcel(
@NonNull android.os.Parcel in) {
return new ProfileNetworkPreference(in);
}
};
}