Merge "[CM] Allow timeout in request network"

am: 43d964ac8f

Change-Id: Ic7a3da0a5292be5e13abca9c98c422bc2f50f3a0
This commit is contained in:
Etan Cohen
2017-03-07 16:49:53 +00:00
committed by android-build-merger

View File

@@ -2614,10 +2614,12 @@ public class ConnectivityManager {
public void onLost(Network network) {} public void onLost(Network network) {}
/** /**
* Called if no network is found in the given timeout time. If no timeout is given, * Called if no network is found in the timeout time specified in
* this will not be called. The associated {@link NetworkRequest} will have already * {@link #requestNetwork(NetworkRequest, int, NetworkCallback)} call. This callback is not
* been removed and released, as if {@link #unregisterNetworkCallback} had been called. * called for the version of {@link #requestNetwork(NetworkRequest, NetworkCallback)}
* @hide * 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() {} public void onUnavailable() {}
@@ -2900,7 +2902,9 @@ public class ConnectivityManager {
* Request a network to satisfy a set of {@link android.net.NetworkCapabilities}. * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}.
* *
* This {@link NetworkRequest} will live until released via * 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 * Status of the request can be followed by listening to the various
* callbacks described in {@link NetworkCallback}. The {@link Network} * callbacks described in {@link NetworkCallback}. The {@link Network}
* can be used to direct traffic to the network. * can be used to direct traffic to the network.
@@ -2933,7 +2937,9 @@ public class ConnectivityManager {
* Request a network to satisfy a set of {@link android.net.NetworkCapabilities}. * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}.
* *
* This {@link NetworkRequest} will live until released via * 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 * Status of the request can be followed by listening to the various
* callbacks described in {@link NetworkCallback}. The {@link Network} * callbacks described in {@link NetworkCallback}. The {@link Network}
* can be used to direct traffic to the network. * can be used to direct traffic to the network.
@@ -2967,13 +2973,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 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}, limited
* by a timeout. * by a timeout.
* *
* This function behaves identically to the non-timedout version, but if a suitable * This function behaves identically to the non-timed-out version
* network is not found within the given time (in milliseconds) the * {@link #requestNetwork(NetworkRequest, NetworkCallback)}, but if a suitable network
* {@link NetworkCallback#onUnavailable()} callback is called. The request must * is not found within the given time (in milliseconds) the
* 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) - 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 * <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
@@ -2981,15 +2999,56 @@ public class ConnectivityManager {
* {@link android.provider.Settings.System#canWrite}.</p> * {@link android.provider.Settings.System#canWrite}.</p>
* *
* @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 callbacks to be utilized for this request. Note
* the callback must not be shared - it uniquely specifies this request. * the callbacks must not be shared - they uniquely specify
* The callback is invoked on the default internal Handler. * this request.
* @param timeoutMs The time in milliseconds to attempt looking for a suitable network * @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 * @hide
*/ */
public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback, public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback,
int timeoutMs) { 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); int legacyType = inferLegacyTypeForNetworkCapabilities(request.networkCapabilities);
requestNetwork(request, networkCallback, timeoutMs, legacyType, getDefaultHandler()); requestNetwork(request, networkCallback, timeoutMs, legacyType, getDefaultHandler());
} }
@@ -3001,8 +3060,14 @@ public class ConnectivityManager {
* *
* This function behaves identically to the non-timedout version, but if a suitable * This function behaves identically to the non-timedout version, but if a suitable
* network is not found within the given time (in milliseconds) the * network is not found within the given time (in milliseconds) the
* {@link NetworkCallback#onUnavailable} callback is called. The request must * {@link NetworkCallback#onUnavailable} callback is called. The request can still be
* still be released normally by calling {@link unregisterNetworkCallback(NetworkCallback)}. * 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 * <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
@@ -3010,16 +3075,19 @@ public class ConnectivityManager {
* {@link android.provider.Settings.System#canWrite}.</p> * {@link android.provider.Settings.System#canWrite}.</p>
* *
* @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
* the callback must not be shared - it uniquely specifies this request.
* @param timeoutMs The time in milliseconds to attempt looking for a suitable network * @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.
* @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. * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
* *
* @hide * @hide
*/ */
public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback, public void requestNetwork(NetworkRequest request, int timeoutMs,
int timeoutMs, Handler handler) { NetworkCallback networkCallback, Handler handler) {
if (timeoutMs <= 0) {
throw new IllegalArgumentException("Non-positive timeoutMs");
}
int legacyType = inferLegacyTypeForNetworkCapabilities(request.networkCapabilities); int legacyType = inferLegacyTypeForNetworkCapabilities(request.networkCapabilities);
CallbackHandler cbHandler = new CallbackHandler(handler); CallbackHandler cbHandler = new CallbackHandler(handler);
requestNetwork(request, networkCallback, timeoutMs, legacyType, cbHandler); requestNetwork(request, networkCallback, timeoutMs, legacyType, cbHandler);