diff options
| author | 2017-03-01 12:47:28 -0800 | |
|---|---|---|
| committer | 2017-03-02 08:06:27 -0800 | |
| commit | aebf17e7f1bc6ca6da9ed5d6c129264590ea2f9e (patch) | |
| tree | e2a0075bfaaaef5e74b56a4497783252e16727a1 | |
| parent | 7f0300f1e20fc3c4c6c063eebcd0d4d1b407578c (diff) | |
[CM] Allow timeout in request network
Add (unhide) a public API which provides network requests with a
timeout. When timed-out the (newly unhidden) onUnavailable() callback
is triggered.
Note: this CL does not add a handler to the API to be consistent
with the existing APIs. There is a separate effort (b/32130437)
to update these APIs with Handlers.
Bug: 31399536
Test: unit tests and CTS (new)
Change-Id: I45ce9ada63372cb56937bb620bfbb7729e5e25d2
| -rw-r--r-- | api/current.txt | 2 | ||||
| -rw-r--r-- | api/system-current.txt | 2 | ||||
| -rw-r--r-- | api/test-current.txt | 2 | ||||
| -rw-r--r-- | core/java/android/net/ConnectivityManager.java | 108 |
4 files changed, 94 insertions, 20 deletions
diff --git a/api/current.txt b/api/current.txt index 0c20240150ae..17b4eb8cc3ca 100644 --- a/api/current.txt +++ b/api/current.txt @@ -23642,6 +23642,7 @@ package android.net { method public void reportNetworkConnectivity(android.net.Network, boolean); method public boolean requestBandwidthUpdate(android.net.Network); method public void requestNetwork(android.net.NetworkRequest, android.net.ConnectivityManager.NetworkCallback); + method public void requestNetwork(android.net.NetworkRequest, int, android.net.ConnectivityManager.NetworkCallback); method public void requestNetwork(android.net.NetworkRequest, android.app.PendingIntent); method public deprecated boolean requestRouteToHost(int, int); method public deprecated void setNetworkPreference(int); @@ -23689,6 +23690,7 @@ package android.net { method public void onLinkPropertiesChanged(android.net.Network, android.net.LinkProperties); method public void onLosing(android.net.Network, int); method public void onLost(android.net.Network); + method public void onUnavailable(); } public static abstract interface ConnectivityManager.OnNetworkActiveListener { diff --git a/api/system-current.txt b/api/system-current.txt index d6b6ad4a18d5..e0176e2c4f4d 100644 --- a/api/system-current.txt +++ b/api/system-current.txt @@ -25429,6 +25429,7 @@ package android.net { method public void reportNetworkConnectivity(android.net.Network, boolean); method public boolean requestBandwidthUpdate(android.net.Network); method public void requestNetwork(android.net.NetworkRequest, android.net.ConnectivityManager.NetworkCallback); + method public void requestNetwork(android.net.NetworkRequest, int, android.net.ConnectivityManager.NetworkCallback); method public void requestNetwork(android.net.NetworkRequest, android.app.PendingIntent); method public deprecated boolean requestRouteToHost(int, int); method public deprecated void setNetworkPreference(int); @@ -25482,6 +25483,7 @@ package android.net { method public void onLinkPropertiesChanged(android.net.Network, android.net.LinkProperties); method public void onLosing(android.net.Network, int); method public void onLost(android.net.Network); + method public void onUnavailable(); } public static abstract interface ConnectivityManager.OnNetworkActiveListener { diff --git a/api/test-current.txt b/api/test-current.txt index 7aaab5bae645..5ce75a1100a7 100644 --- a/api/test-current.txt +++ b/api/test-current.txt @@ -23715,6 +23715,7 @@ package android.net { method public void reportNetworkConnectivity(android.net.Network, boolean); method public boolean requestBandwidthUpdate(android.net.Network); method public void requestNetwork(android.net.NetworkRequest, android.net.ConnectivityManager.NetworkCallback); + method public void requestNetwork(android.net.NetworkRequest, int, android.net.ConnectivityManager.NetworkCallback); method public void requestNetwork(android.net.NetworkRequest, android.app.PendingIntent); method public deprecated boolean requestRouteToHost(int, int); method public deprecated void setNetworkPreference(int); @@ -23762,6 +23763,7 @@ package android.net { method public void onLinkPropertiesChanged(android.net.Network, android.net.LinkProperties); method public void onLosing(android.net.Network, int); method public void onLost(android.net.Network); + method public void onUnavailable(); } public static abstract interface ConnectivityManager.OnNetworkActiveListener { diff --git a/core/java/android/net/ConnectivityManager.java b/core/java/android/net/ConnectivityManager.java index 30afdc2768c5..af9b9b2d656a 100644 --- a/core/java/android/net/ConnectivityManager.java +++ b/core/java/android/net/ConnectivityManager.java @@ -2647,10 +2647,12 @@ public class ConnectivityManager { public void onLost(Network network) {} /** - * Called if no network is found in the given timeout time. If no timeout is given, - * this will not be called. The associated {@link NetworkRequest} will have already - * been removed and released, as if {@link #unregisterNetworkCallback} had been called. - * @hide + * Called if no network is found in the timeout time specified in + * {@link #requestNetwork(NetworkRequest, int, NetworkCallback)} call. This callback is not + * called for the version of {@link #requestNetwork(NetworkRequest, NetworkCallback)} + * without timeout. When this callback is invoked the associated + * {@link NetworkRequest} will have already been removed and released, as if + * {@link #unregisterNetworkCallback(NetworkCallback)} had been called. */ public void onUnavailable() {} @@ -2933,7 +2935,9 @@ public class ConnectivityManager { * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}. * * This {@link NetworkRequest} will live until released via - * {@link #unregisterNetworkCallback(NetworkCallback)} or the calling application exits. + * {@link #unregisterNetworkCallback(NetworkCallback)} or the calling application exits. A + * version of the method which takes a timeout is + * {@link #requestNetwork(NetworkRequest, int, NetworkCallback)}. * Status of the request can be followed by listening to the various * callbacks described in {@link NetworkCallback}. The {@link Network} * can be used to direct traffic to the network. @@ -2966,7 +2970,9 @@ public class ConnectivityManager { * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}. * * This {@link NetworkRequest} will live until released via - * {@link #unregisterNetworkCallback(NetworkCallback)} or the calling application exits. + * {@link #unregisterNetworkCallback(NetworkCallback)} or the calling application exits. A + * version of the method which takes a timeout is + * {@link #requestNetwork(NetworkRequest, int, NetworkCallback)}. * Status of the request can be followed by listening to the various * callbacks described in {@link NetworkCallback}. The {@link Network} * can be used to direct traffic to the network. @@ -3000,13 +3006,25 @@ public class ConnectivityManager { } /** + * Note: this is a deprecated version of + * {@link #requestNetwork(NetworkRequest, int, NetworkCallback)} - please transition code to use + * the unhidden version of the function. + * TODO: replace all callers with the new version of the API + * * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}, limited * by a timeout. * - * This function behaves identically to the non-timedout version, but if a suitable - * network is not found within the given time (in milliseconds) the - * {@link NetworkCallback#onUnavailable()} callback is called. The request must - * still be released normally by calling {@link #unregisterNetworkCallback(NetworkCallback)}. + * This function behaves identically to the non-timed-out version + * {@link #requestNetwork(NetworkRequest, NetworkCallback)}, but if a suitable network + * is not found within the given time (in milliseconds) the + * {@link NetworkCallback#onUnavailable()} callback is called. The request can still be + * released normally by calling {@link #unregisterNetworkCallback(NetworkCallback)} but does + * not have to be released if timed-out (it is automatically released). Unregistering a + * request that timed out is not an error. + * + * <p>Do not use this method to poll for the existence of specific networks (e.g. with a small + * timeout) - the {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} is provided + * for that purpose. Calling this method will attempt to bring up the requested network. * * <p>This method requires the caller to hold either the * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission @@ -3014,15 +3032,56 @@ public class ConnectivityManager { * {@link android.provider.Settings.System#canWrite}.</p> * * @param request {@link NetworkRequest} describing this request. - * @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 is invoked on the default internal Handler. + * @param networkCallback The callbacks to be utilized for this request. Note + * the callbacks must not be shared - they uniquely specify + * this request. * @param timeoutMs The time in milliseconds to attempt looking for a suitable network - * before {@link NetworkCallback#onUnavailable()} is called. + * before {@link NetworkCallback#onUnavailable()} is called. The timeout must + * be a positive value (i.e. >0). * @hide */ public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback, int timeoutMs) { + if (timeoutMs <= 0) { + throw new IllegalArgumentException("Non-positive timeoutMs: " + timeoutMs); + } + int legacyType = inferLegacyTypeForNetworkCapabilities(request.networkCapabilities); + requestNetwork(request, networkCallback, timeoutMs, legacyType, getDefaultHandler()); + } + + /** + * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}, limited + * by a timeout. + * + * This function behaves identically to the non-timed-out version + * {@link #requestNetwork(NetworkRequest, NetworkCallback)}, but if a suitable network + * is not found within the given time (in milliseconds) the + * {@link NetworkCallback#onUnavailable()} callback is called. The request can still be + * released normally by calling {@link #unregisterNetworkCallback(NetworkCallback)} but does + * not have to be released if timed-out (it is automatically released). Unregistering a + * request that timed out is not an error. + * + * <p>Do not use this method to poll for the existence of specific networks (e.g. with a small + * timeout) - {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} is provided + * for that purpose. Calling this method will attempt to bring up the requested network. + * + * <p>This method requires the caller to hold either the + * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission + * or the ability to modify system settings as determined by + * {@link android.provider.Settings.System#canWrite}.</p> + * + * @param request {@link NetworkRequest} describing this request. + * @param timeoutMs The time in milliseconds to attempt looking for a suitable network + * before {@link NetworkCallback#onUnavailable()} is called. The timeout must + * be a positive value (i.e. >0). + * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note + * the callback must not be shared - it uniquely specifies this request. + */ + public void requestNetwork(NetworkRequest request, int timeoutMs, + NetworkCallback networkCallback) { + if (timeoutMs <= 0) { + throw new IllegalArgumentException("Non-positive timeoutMs: " + timeoutMs); + } int legacyType = inferLegacyTypeForNetworkCapabilities(request.networkCapabilities); requestNetwork(request, networkCallback, timeoutMs, legacyType, getDefaultHandler()); } @@ -3034,8 +3093,14 @@ public class ConnectivityManager { * * This function behaves identically to the non-timedout version, but if a suitable * network is not found within the given time (in milliseconds) the - * {@link NetworkCallback#onUnavailable} callback is called. The request must - * still be released normally by calling {@link unregisterNetworkCallback(NetworkCallback)}. + * {@link NetworkCallback#onUnavailable} callback is called. The request can still be + * released normally by calling {@link #unregisterNetworkCallback(NetworkCallback)} but does + * not have to be released if timed-out (it is automatically released). Unregistering a + * request that timed out is not an error. + * + * <p>Do not use this method to poll for the existence of specific networks (e.g. with a small + * timeout) - {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} is provided + * for that purpose. Calling this method will attempt to bring up the requested network. * * <p>This method requires the caller to hold either the * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission @@ -3043,16 +3108,19 @@ public class ConnectivityManager { * {@link android.provider.Settings.System#canWrite}.</p> * * @param request {@link NetworkRequest} describing this request. - * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note - * the callback must not be shared - it uniquely specifies this request. * @param timeoutMs The time in milliseconds to attempt looking for a suitable network * before {@link NetworkCallback#onUnavailable} is called. + * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note + * the callback must not be shared - it uniquely specifies this request. * @param handler {@link Handler} to specify the thread upon which the callback will be invoked. * * @hide */ - public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback, - int timeoutMs, Handler handler) { + public void requestNetwork(NetworkRequest request, int timeoutMs, + NetworkCallback networkCallback, Handler handler) { + if (timeoutMs <= 0) { + throw new IllegalArgumentException("Non-positive timeoutMs"); + } int legacyType = inferLegacyTypeForNetworkCapabilities(request.networkCapabilities); CallbackHandler cbHandler = new CallbackHandler(handler); requestNetwork(request, networkCallback, timeoutMs, legacyType, cbHandler); |