Document the limitation to a hundred callbacks.

Some developers have been surprised by this limitation and had trouble
figuring out what the issue was. Add documentation to address this.

This also includes a drive-by removal of a duplicate check.

Bug: 149867479
Test: doc-only change
Original-Change: https://android-review.googlesource.com/1313813
Merged-In: I5911d01984695550b6c9afe7a8eb535bf5e320a1
Change-Id: I5911d01984695550b6c9afe7a8eb535bf5e320a1
This commit is contained in:
Chalard Jean
2020-06-11 13:03:37 +00:00
parent f949ec251f
commit cece40543e
3 changed files with 87 additions and 10 deletions

View File

@@ -711,6 +711,13 @@ public class ConnectivityDiagnosticsManager {
* not currently registered. If a ConnectivityDiagnosticsCallback instance is registered with * not currently registered. If a ConnectivityDiagnosticsCallback instance is registered with
* multiple NetworkRequests, an IllegalArgumentException will be thrown. * multiple NetworkRequests, an IllegalArgumentException will be thrown.
* *
* <p>To avoid performance issues due to apps leaking callbacks, the system will limit the
* number of outstanding requests to 100 per app (identified by their UID), shared with
* callbacks in {@link ConnectivityManager}. Registering a callback with this method will count
* toward this limit. If this limit is exceeded, an exception will be thrown. To avoid hitting
* this issue and to conserve resources, make sure to unregister the callbacks with
* {@link #unregisterConnectivityDiagnosticsCallback}.
*
* @param request The NetworkRequest that will be used to match with Networks for which * @param request The NetworkRequest that will be used to match with Networks for which
* callbacks will be fired * callbacks will be fired
* @param e The Executor to be used for running the callback method invocations * @param e The Executor to be used for running the callback method invocations
@@ -718,6 +725,7 @@ public class ConnectivityDiagnosticsManager {
* System * System
* @throws IllegalArgumentException if the same callback instance is registered with multiple * @throws IllegalArgumentException if the same callback instance is registered with multiple
* NetworkRequests * NetworkRequests
* @throws RuntimeException if the app already has too many callbacks registered.
*/ */
public void registerConnectivityDiagnosticsCallback( public void registerConnectivityDiagnosticsCallback(
@NonNull NetworkRequest request, @NonNull NetworkRequest request,

View File

@@ -3794,13 +3794,22 @@ public class ConnectivityManager {
* or the ability to modify system settings as determined by * or the ability to modify system settings as determined by
* {@link android.provider.Settings.System#canWrite}.</p> * {@link android.provider.Settings.System#canWrite}.</p>
* *
* <p>To avoid performance issues due to apps leaking callbacks, the system will limit the
* number of outstanding requests to 100 per app (identified by their UID), shared with
* all variants of this method, of {@link #registerNetworkCallback} as well as
* {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}.
* Requesting a network with this method will count toward this limit. If this limit is
* exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources,
* make sure to unregister the callbacks with
* {@link #unregisterNetworkCallback(NetworkCallback)}.
*
* @param request {@link NetworkRequest} describing this request. * @param request {@link NetworkRequest} describing this request.
* @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
* the callback must not be shared - it uniquely specifies this request. * the callback must not be shared - it uniquely specifies this request.
* The callback is invoked on the default internal Handler. * The callback is invoked on the default internal Handler.
* @throws IllegalArgumentException if {@code request} contains invalid network capabilities. * @throws IllegalArgumentException if {@code request} contains invalid network capabilities.
* @throws SecurityException if missing the appropriate permissions. * @throws SecurityException if missing the appropriate permissions.
* @throws RuntimeException if request limit per UID is exceeded. * @throws RuntimeException if the app already has too many callbacks registered.
*/ */
public void requestNetwork(@NonNull NetworkRequest request, public void requestNetwork(@NonNull NetworkRequest request,
@NonNull NetworkCallback networkCallback) { @NonNull NetworkCallback networkCallback) {
@@ -3814,8 +3823,8 @@ public class ConnectivityManager {
* but runs all the callbacks on the passed Handler. * but runs all the callbacks on the passed Handler.
* *
* <p>This method has the same permission requirements as * <p>This method has the same permission requirements as
* {@link #requestNetwork(NetworkRequest, NetworkCallback)} and throws the same exceptions in * {@link #requestNetwork(NetworkRequest, NetworkCallback)}, is subject to the same limitations,
* the same conditions. * and throws the same exceptions in the same conditions.
* *
* @param request {@link NetworkRequest} describing this request. * @param request {@link NetworkRequest} describing this request.
* @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
@@ -3846,8 +3855,8 @@ public class ConnectivityManager {
* for that purpose. Calling this method will attempt to bring up the requested network. * for that purpose. Calling this method will attempt to bring up the requested network.
* *
* <p>This method has the same permission requirements as * <p>This method has the same permission requirements as
* {@link #requestNetwork(NetworkRequest, NetworkCallback)} and throws the same exceptions in * {@link #requestNetwork(NetworkRequest, NetworkCallback)}, is subject to the same limitations,
* the same conditions. * and throws the same exceptions in the same conditions.
* *
* @param request {@link NetworkRequest} describing this request. * @param request {@link NetworkRequest} describing this request.
* @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
@@ -3873,8 +3882,8 @@ public class ConnectivityManager {
* on the passed Handler. * on the passed Handler.
* *
* <p>This method has the same permission requirements as * <p>This method has the same permission requirements as
* {@link #requestNetwork(NetworkRequest, NetworkCallback, int)} and throws the same exceptions * {@link #requestNetwork(NetworkRequest, NetworkCallback)}, is subject to the same limitations,
* in the same conditions. * and throws the same exceptions in the same conditions.
* *
* @param request {@link NetworkRequest} describing this request. * @param request {@link NetworkRequest} describing this request.
* @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
@@ -3943,6 +3952,15 @@ public class ConnectivityManager {
* is unknown prior to bringing up the network so the framework does not * is unknown prior to bringing up the network so the framework does not
* know how to go about satisfying a request with these capabilities. * know how to go about satisfying a request with these capabilities.
* *
* <p>To avoid performance issues due to apps leaking callbacks, the system will limit the
* number of outstanding requests to 100 per app (identified by their UID), shared with
* all variants of this method, of {@link #registerNetworkCallback} as well as
* {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}.
* Requesting a network with this method will count toward this limit. If this limit is
* exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources,
* make sure to unregister the callbacks with {@link #unregisterNetworkCallback(PendingIntent)}
* or {@link #releaseNetworkRequest(PendingIntent)}.
*
* <p>This method requires the caller to hold either the * <p>This method requires the caller to hold either the
* {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
* or the ability to modify system settings as determined by * or the ability to modify system settings as determined by
@@ -3954,7 +3972,7 @@ public class ConnectivityManager {
* comes from {@link PendingIntent#getBroadcast}. Cannot be null. * comes from {@link PendingIntent#getBroadcast}. Cannot be null.
* @throws IllegalArgumentException if {@code request} contains invalid network capabilities. * @throws IllegalArgumentException if {@code request} contains invalid network capabilities.
* @throws SecurityException if missing the appropriate permissions. * @throws SecurityException if missing the appropriate permissions.
* @throws RuntimeException if request limit per UID is exceeded. * @throws RuntimeException if the app already has too many callbacks registered.
*/ */
public void requestNetwork(@NonNull NetworkRequest request, public void requestNetwork(@NonNull NetworkRequest request,
@NonNull PendingIntent operation) { @NonNull PendingIntent operation) {
@@ -4010,10 +4028,20 @@ public class ConnectivityManager {
* either the application exits or {@link #unregisterNetworkCallback(NetworkCallback)} is * either the application exits or {@link #unregisterNetworkCallback(NetworkCallback)} is
* called. * called.
* *
* <p>To avoid performance issues due to apps leaking callbacks, the system will limit the
* number of outstanding requests to 100 per app (identified by their UID), shared with
* all variants of this method, of {@link #requestNetwork} as well as
* {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}.
* Requesting a network with this method will count toward this limit. If this limit is
* exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources,
* make sure to unregister the callbacks with
* {@link #unregisterNetworkCallback(NetworkCallback)}.
*
* @param request {@link NetworkRequest} describing this request. * @param request {@link NetworkRequest} describing this request.
* @param networkCallback The {@link NetworkCallback} that the system will call as suitable * @param networkCallback The {@link NetworkCallback} that the system will call as suitable
* networks change state. * networks change state.
* The callback is invoked on the default internal Handler. * The callback is invoked on the default internal Handler.
* @throws RuntimeException if the app already has too many callbacks registered.
*/ */
@RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
public void registerNetworkCallback(@NonNull NetworkRequest request, public void registerNetworkCallback(@NonNull NetworkRequest request,
@@ -4027,10 +4055,21 @@ public class ConnectivityManager {
* either the application exits or {@link #unregisterNetworkCallback(NetworkCallback)} is * either the application exits or {@link #unregisterNetworkCallback(NetworkCallback)} is
* called. * called.
* *
* <p>To avoid performance issues due to apps leaking callbacks, the system will limit the
* number of outstanding requests to 100 per app (identified by their UID), shared with
* all variants of this method, of {@link #requestNetwork} as well as
* {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}.
* Requesting a network with this method will count toward this limit. If this limit is
* exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources,
* make sure to unregister the callbacks with
* {@link #unregisterNetworkCallback(NetworkCallback)}.
*
*
* @param request {@link NetworkRequest} describing this request. * @param request {@link NetworkRequest} describing this request.
* @param networkCallback The {@link NetworkCallback} that the system will call as suitable * @param networkCallback The {@link NetworkCallback} that the system will call as suitable
* networks change state. * networks change state.
* @param handler {@link Handler} to specify the thread upon which the callback will be invoked. * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
* @throws RuntimeException if the app already has too many callbacks registered.
*/ */
@RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
public void registerNetworkCallback(@NonNull NetworkRequest request, public void registerNetworkCallback(@NonNull NetworkRequest request,
@@ -4064,10 +4103,21 @@ public class ConnectivityManager {
* <p> * <p>
* The request may be released normally by calling * The request may be released normally by calling
* {@link #unregisterNetworkCallback(android.app.PendingIntent)}. * {@link #unregisterNetworkCallback(android.app.PendingIntent)}.
*
* <p>To avoid performance issues due to apps leaking callbacks, the system will limit the
* number of outstanding requests to 100 per app (identified by their UID), shared with
* all variants of this method, of {@link #requestNetwork} as well as
* {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}.
* Requesting a network with this method will count toward this limit. If this limit is
* exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources,
* make sure to unregister the callbacks with {@link #unregisterNetworkCallback(PendingIntent)}
* or {@link #releaseNetworkRequest(PendingIntent)}.
*
* @param request {@link NetworkRequest} describing this request. * @param request {@link NetworkRequest} describing this request.
* @param operation Action to perform when the network is available (corresponds * @param operation Action to perform when the network is available (corresponds
* to the {@link NetworkCallback#onAvailable} call. Typically * to the {@link NetworkCallback#onAvailable} call. Typically
* comes from {@link PendingIntent#getBroadcast}. Cannot be null. * comes from {@link PendingIntent#getBroadcast}. Cannot be null.
* @throws RuntimeException if the app already has too many callbacks registered.
*/ */
@RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
public void registerNetworkCallback(@NonNull NetworkRequest request, public void registerNetworkCallback(@NonNull NetworkRequest request,
@@ -4089,9 +4139,19 @@ public class ConnectivityManager {
* will continue to be called until either the application exits or * will continue to be called until either the application exits or
* {@link #unregisterNetworkCallback(NetworkCallback)} is called. * {@link #unregisterNetworkCallback(NetworkCallback)} is called.
* *
* <p>To avoid performance issues due to apps leaking callbacks, the system will limit the
* number of outstanding requests to 100 per app (identified by their UID), shared with
* all variants of this method, of {@link #requestNetwork} as well as
* {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}.
* Requesting a network with this method will count toward this limit. If this limit is
* exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources,
* make sure to unregister the callbacks with
* {@link #unregisterNetworkCallback(NetworkCallback)}.
*
* @param networkCallback The {@link NetworkCallback} that the system will call as the * @param networkCallback The {@link NetworkCallback} that the system will call as the
* system default network changes. * system default network changes.
* The callback is invoked on the default internal Handler. * The callback is invoked on the default internal Handler.
* @throws RuntimeException if the app already has too many callbacks registered.
*/ */
@RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
public void registerDefaultNetworkCallback(@NonNull NetworkCallback networkCallback) { public void registerDefaultNetworkCallback(@NonNull NetworkCallback networkCallback) {
@@ -4103,9 +4163,19 @@ public class ConnectivityManager {
* will continue to be called until either the application exits or * will continue to be called until either the application exits or
* {@link #unregisterNetworkCallback(NetworkCallback)} is called. * {@link #unregisterNetworkCallback(NetworkCallback)} is called.
* *
* <p>To avoid performance issues due to apps leaking callbacks, the system will limit the
* number of outstanding requests to 100 per app (identified by their UID), shared with
* all variants of this method, of {@link #requestNetwork} as well as
* {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}.
* Requesting a network with this method will count toward this limit. If this limit is
* exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources,
* make sure to unregister the callbacks with
* {@link #unregisterNetworkCallback(NetworkCallback)}.
*
* @param networkCallback The {@link NetworkCallback} that the system will call as the * @param networkCallback The {@link NetworkCallback} that the system will call as the
* system default network changes. * system default network changes.
* @param handler {@link Handler} to specify the thread upon which the callback will be invoked. * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
* @throws RuntimeException if the app already has too many callbacks registered.
*/ */
@RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
public void registerDefaultNetworkCallback(@NonNull NetworkCallback networkCallback, public void registerDefaultNetworkCallback(@NonNull NetworkCallback networkCallback,
@@ -4197,7 +4267,6 @@ public class ConnectivityManager {
* Cannot be null. * Cannot be null.
*/ */
public void unregisterNetworkCallback(@NonNull PendingIntent operation) { public void unregisterNetworkCallback(@NonNull PendingIntent operation) {
checkPendingIntentNotNull(operation);
releaseNetworkRequest(operation); releaseNetworkRequest(operation);
} }

View File

@@ -6426,7 +6426,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
final boolean shouldFilter = requiresVpnIsolation(nai, newNc, nai.linkProperties); final boolean shouldFilter = requiresVpnIsolation(nai, newNc, nai.linkProperties);
final String iface = nai.linkProperties.getInterfaceName(); final String iface = nai.linkProperties.getInterfaceName();
// For VPN uid interface filtering, old ranges need to be removed before new ranges can // For VPN uid interface filtering, old ranges need to be removed before new ranges can
// be added, due to the range being expanded and stored as invidiual UIDs. For example // be added, due to the range being expanded and stored as individual UIDs. For example
// the UIDs might be updated from [0, 99999] to ([0, 10012], [10014, 99999]) which means // the UIDs might be updated from [0, 99999] to ([0, 10012], [10014, 99999]) which means
// prevRanges = [0, 99999] while newRanges = [0, 10012], [10014, 99999]. If prevRanges // prevRanges = [0, 99999] while newRanges = [0, 10012], [10014, 99999]. If prevRanges
// were added first and then newRanges got removed later, there would be only one uid // were added first and then newRanges got removed later, there would be only one uid