Merge changes I335e82e2,I84ba363d,I8f18083b,I854a952d,I00e23441

* changes:
  Remove per-user preference when the user is removed
  Expose the enterprise per-profile networking API.
  Implement setNetworkPreferenceForUser.
  Public API for per-profile network preference.
  Add tests for setNetworkPreferenceForUser
This commit is contained in:
Chalard Jean
2021-03-17 05:48:18 +00:00
committed by Gerrit Code Review
9 changed files with 827 additions and 64 deletions

View File

@@ -64,6 +64,7 @@ import android.os.RemoteException;
import android.os.ResultReceiver;
import android.os.ServiceManager;
import android.os.ServiceSpecificException;
import android.os.UserHandle;
import android.provider.Settings;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
@@ -970,6 +971,33 @@ public class ConnectivityManager {
}
}
/**
* Preference for {@link #setNetworkPreferenceForUser(UserHandle, int, Executor, Runnable)}.
* Specify that the traffic for this user should by follow the default rules.
* @hide
*/
@SystemApi
public static final int PROFILE_NETWORK_PREFERENCE_DEFAULT = 0;
/**
* Preference for {@link #setNetworkPreferenceForUser(UserHandle, int, Executor, Runnable)}.
* Specify that the traffic for this user should by default go on a network with
* {@link NetworkCapabilities#NET_CAPABILITY_ENTERPRISE}, and on the system default network
* if no such network is available.
* @hide
*/
@SystemApi
public static final int PROFILE_NETWORK_PREFERENCE_ENTERPRISE = 1;
/** @hide */
@Retention(RetentionPolicy.SOURCE)
@IntDef(value = {
PROFILE_NETWORK_PREFERENCE_DEFAULT,
PROFILE_NETWORK_PREFERENCE_ENTERPRISE
})
public @interface ProfileNetworkPreference {
}
/**
* Specifies the preferred network type. When the device has more
* than one type available the preferred network type will be used.
@@ -5066,19 +5094,6 @@ public class ConnectivityManager {
TYPE_NONE, new CallbackHandler(handler));
}
/**
* Listener for {@link #setOemNetworkPreference(OemNetworkPreferences, Executor,
* OnSetOemNetworkPreferenceListener)}.
* @hide
*/
@SystemApi
public interface OnSetOemNetworkPreferenceListener {
/**
* Called when setOemNetworkPreference() successfully completes.
*/
void onComplete();
}
/**
* Used by automotive devices to set the network preferences used to direct traffic at an
* application level as per the given OemNetworkPreferences. An example use-case would be an
@@ -5101,16 +5116,16 @@ public class ConnectivityManager {
@RequiresPermission(android.Manifest.permission.CONTROL_OEM_PAID_NETWORK_PREFERENCE)
public void setOemNetworkPreference(@NonNull final OemNetworkPreferences preference,
@Nullable @CallbackExecutor final Executor executor,
@Nullable final OnSetOemNetworkPreferenceListener listener) {
@Nullable final Runnable listener) {
Objects.requireNonNull(preference, "OemNetworkPreferences must be non-null");
if (null != listener) {
Objects.requireNonNull(executor, "Executor must be non-null");
}
final IOnSetOemNetworkPreferenceListener listenerInternal = listener == null ? null :
new IOnSetOemNetworkPreferenceListener.Stub() {
final IOnCompleteListener listenerInternal = listener == null ? null :
new IOnCompleteListener.Stub() {
@Override
public void onComplete() {
executor.execute(listener::onComplete);
executor.execute(listener::run);
}
};
@@ -5122,6 +5137,52 @@ public class ConnectivityManager {
}
}
/**
* Request that a user profile is put by default on a network matching a given preference.
*
* See the documentation for the individual preferences for a description of the supported
* behaviors.
*
* @param profile the profile concerned.
* @param preference the preference for this 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
*/
// This function is for establishing per-profile default networking and can only be called by
// the device policy manager, running as the system server. It would make no sense to call it
// on a context for a user because it does not establish a setting on behalf of a user, rather
// it establishes a setting for a user on behalf of the DPM.
@SuppressLint({"UserHandle"})
@SystemApi(client = MODULE_LIBRARIES)
@RequiresPermission(android.Manifest.permission.NETWORK_STACK)
public void setProfileNetworkPreference(@NonNull final UserHandle profile,
@ProfileNetworkPreference final int preference,
@Nullable @CallbackExecutor final Executor executor,
@Nullable final Runnable listener) {
if (null != listener) {
Objects.requireNonNull(executor, "Pass a non-null executor, or a null listener");
}
final IOnCompleteListener proxy;
if (null == listener) {
proxy = null;
} else {
proxy = new IOnCompleteListener.Stub() {
@Override
public void onComplete() {
executor.execute(listener::run);
}
};
}
try {
mService.setProfileNetworkPreference(profile, preference, proxy);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
// The first network ID of IPSec tunnel interface.
private static final int TUN_INTF_NETID_START = 0xFC00; // 0xFC00 = 64512
// The network ID range of IPSec tunnel interface.

View File

@@ -20,7 +20,7 @@ import android.app.PendingIntent;
import android.net.ConnectionInfo;
import android.net.ConnectivityDiagnosticsManager;
import android.net.IConnectivityDiagnosticsCallback;
import android.net.IOnSetOemNetworkPreferenceListener;
import android.net.IOnCompleteListener;
import android.net.INetworkActivityListener;
import android.net.IQosCallback;
import android.net.ISocketKeepaliveCallback;
@@ -43,6 +43,7 @@ import android.os.Messenger;
import android.os.ParcelFileDescriptor;
import android.os.PersistableBundle;
import android.os.ResultReceiver;
import android.os.UserHandle;
import com.android.connectivity.aidl.INetworkAgent;
@@ -215,5 +216,8 @@ interface IConnectivityManager
void unregisterQosCallback(in IQosCallback callback);
void setOemNetworkPreference(in OemNetworkPreferences preference,
in IOnSetOemNetworkPreferenceListener listener);
in IOnCompleteListener listener);
void setProfileNetworkPreference(in UserHandle profile, int preference,
in IOnCompleteListener listener);
}

View File

@@ -1,23 +0,0 @@
/**
*
* 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;
/** @hide */
oneway interface IOnSetOemNetworkPreferenceListener {
void onComplete();
}

View File

@@ -72,6 +72,14 @@ public final class OemNetworkPreferences implements Parcelable {
@NonNull
private final Bundle mNetworkMappings;
/**
* Return whether this object is empty.
* @hide
*/
public boolean isEmpty() {
return mNetworkMappings.keySet().size() == 0;
}
/**
* Return the currently built application package name to {@link OemNetworkPreference} mappings.
* @return the current network preferences map.