From ee2f45d85f289a7a352e22cd3912cf121ac7aa6e Mon Sep 17 00:00:00 2001 From: Paul Jensen Date: Tue, 10 Mar 2015 10:54:12 -0400 Subject: [PATCH 1/4] Deprecate static ConnectivityManager.get/setProcessDefaultNetwork() functions. These functions risk hitting an unchecked Exception due to ConnectivityManager not being instantiated yet. Also, change Network.openConnection() to throw a checked Exception rather than an unchecked Exception when ConnectivityManager is not yet instantiated. bug:19416463 Change-Id: Ie1e2b3238aec0343d267c76b64927073f2f05f85 --- .../java/android/net/ConnectivityManager.java | 82 +++++++++++++++---- core/java/android/net/Network.java | 11 ++- core/java/android/net/NetworkUtils.java | 2 +- core/jni/android_net_NetUtils.cpp | 4 +- 4 files changed, 76 insertions(+), 23 deletions(-) diff --git a/core/java/android/net/ConnectivityManager.java b/core/java/android/net/ConnectivityManager.java index a00246fd99..767113e848 100644 --- a/core/java/android/net/ConnectivityManager.java +++ b/core/java/android/net/ConnectivityManager.java @@ -1201,7 +1201,7 @@ public class ConnectivityManager { * @return {@code true} on success, {@code false} on failure * * @deprecated Deprecated in favor of the {@link #requestNetwork}, - * {@link #setProcessDefaultNetwork} and {@link Network#getSocketFactory} api. + * {@link #bindProcessToNetwork} and {@link Network#getSocketFactory} api. */ public boolean requestRouteToHost(int networkType, int hostAddress) { return requestRouteToHostAddress(networkType, NetworkUtils.intToInetAddress(hostAddress)); @@ -1219,7 +1219,7 @@ public class ConnectivityManager { * @return {@code true} on success, {@code false} on failure * @hide * @deprecated Deprecated in favor of the {@link #requestNetwork} and - * {@link #setProcessDefaultNetwork} api. + * {@link #bindProcessToNetwork} api. */ public boolean requestRouteToHostAddress(int networkType, InetAddress hostAddress) { try { @@ -1344,7 +1344,7 @@ public class ConnectivityManager { * listener. *

* If the process default network has been set with - * {@link ConnectivityManager#setProcessDefaultNetwork} this function will not + * {@link ConnectivityManager#bindProcessToNetwork} this function will not * reflect the process's default, but the system default. * * @param l The listener to be told when the network is active. @@ -1429,11 +1429,20 @@ public class ConnectivityManager { * situations where a Context pointer is unavailable. * @hide */ - public static ConnectivityManager getInstance() { - if (sInstance == null) { + static ConnectivityManager getInstanceOrNull() { + return sInstance; + } + + /** + * @deprecated - use getSystemService. This is a kludge to support static access in certain + * situations where a Context pointer is unavailable. + * @hide + */ + private static ConnectivityManager getInstance() { + if (getInstanceOrNull() == null) { throw new IllegalStateException("No ConnectivityManager yet constructed"); } - return sInstance; + return getInstanceOrNull(); } /** @@ -1769,7 +1778,7 @@ public class ConnectivityManager { /** * Get the current default HTTP proxy settings. If a global proxy is set it will be returned, * otherwise if this process is bound to a {@link Network} using - * {@link #setProcessDefaultNetwork} then that {@code Network}'s proxy is returned, otherwise + * {@link #bindProcessToNetwork} then that {@code Network}'s proxy is returned, otherwise * the default network's proxy is returned. * * @return the {@link ProxyInfo} for the current HTTP proxy, or {@code null} if no @@ -1777,7 +1786,7 @@ public class ConnectivityManager { * @hide */ public ProxyInfo getDefaultProxy() { - final Network network = getProcessDefaultNetwork(); + final Network network = getBoundNetworkForProcess(); if (network != null) { final ProxyInfo globalProxy = getGlobalProxy(); if (globalProxy != null) return globalProxy; @@ -2337,9 +2346,8 @@ public class ConnectivityManager { * successfully finding a network for the applications request. Retrieve it with * {@link android.content.Intent#getParcelableExtra(String)}. *

- * Note that if you intend to invoke {@link #setProcessDefaultNetwork} or - * {@link Network#openConnection(java.net.URL)} then you must get a - * ConnectivityManager instance before doing so. + * Note that if you intend to invoke {@link Network#openConnection(java.net.URL)} + * then you must get a ConnectivityManager instance before doing so. */ public static final String EXTRA_NETWORK = "android.net.extra.NETWORK"; @@ -2458,15 +2466,42 @@ public class ConnectivityManager { * Sockets created by Network.getSocketFactory().createSocket() and * performing network-specific host name resolutions via * {@link Network#getAllByName Network.getAllByName} is preferred to calling - * {@code setProcessDefaultNetwork}. + * {@code bindProcessToNetwork}. * * @param network The {@link Network} to bind the current process to, or {@code null} to clear * the current binding. * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid. */ + public boolean bindProcessToNetwork(Network network) { + // Forcing callers to call thru non-static function ensures ConnectivityManager + // instantiated. + return setProcessDefaultNetwork(network); + } + + /** + * Binds the current process to {@code network}. All Sockets created in the future + * (and not explicitly bound via a bound SocketFactory from + * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to + * {@code network}. All host name resolutions will be limited to {@code network} as well. + * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to + * work and all host name resolutions will fail. This is by design so an application doesn't + * accidentally use Sockets it thinks are still bound to a particular {@link Network}. + * To clear binding pass {@code null} for {@code network}. Using individually bound + * Sockets created by Network.getSocketFactory().createSocket() and + * performing network-specific host name resolutions via + * {@link Network#getAllByName Network.getAllByName} is preferred to calling + * {@code setProcessDefaultNetwork}. + * + * @param network The {@link Network} to bind the current process to, or {@code null} to clear + * the current binding. + * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid. + * @deprecated This function can throw {@link IllegalStateException}. Use + * {@link #bindProcessToNetwork} instead. {@code bindProcessToNetwork} + * is a direct replacement. + */ public static boolean setProcessDefaultNetwork(Network network) { int netId = (network == null) ? NETID_UNSET : network.netId; - if (netId == NetworkUtils.getNetworkBoundToProcess()) { + if (netId == NetworkUtils.getBoundNetworkForProcess()) { return true; } if (NetworkUtils.bindProcessToNetwork(netId)) { @@ -2486,19 +2521,34 @@ public class ConnectivityManager { /** * Returns the {@link Network} currently bound to this process via - * {@link #setProcessDefaultNetwork}, or {@code null} if no {@link Network} is explicitly bound. + * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound. * * @return {@code Network} to which this process is bound, or {@code null}. */ + public Network getBoundNetworkForProcess() { + // Forcing callers to call thru non-static function ensures ConnectivityManager + // instantiated. + return getProcessDefaultNetwork(); + } + + /** + * Returns the {@link Network} currently bound to this process via + * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound. + * + * @return {@code Network} to which this process is bound, or {@code null}. + * @deprecated Using this function can lead to other functions throwing + * {@link IllegalStateException}. Use {@link #getBoundNetworkForProcess} instead. + * {@code getBoundNetworkForProcess} is a direct replacement. + */ public static Network getProcessDefaultNetwork() { - int netId = NetworkUtils.getNetworkBoundToProcess(); + int netId = NetworkUtils.getBoundNetworkForProcess(); if (netId == NETID_UNSET) return null; return new Network(netId); } /** * Binds host resolutions performed by this process to {@code network}. - * {@link #setProcessDefaultNetwork} takes precedence over this setting. + * {@link #bindProcessToNetwork} takes precedence over this setting. * * @param network The {@link Network} to bind host resolutions from the current process to, or * {@code null} to clear the current binding. diff --git a/core/java/android/net/Network.java b/core/java/android/net/Network.java index 5c126964ad..df6eb99d46 100644 --- a/core/java/android/net/Network.java +++ b/core/java/android/net/Network.java @@ -51,7 +51,7 @@ import com.android.okhttp.OkHttpClient; * {@link ConnectivityManager#registerNetworkCallback} calls. * It is used to direct traffic to the given {@code Network}, either on a {@link Socket} basis * through a targeted {@link SocketFactory} or process-wide via - * {@link ConnectivityManager#setProcessDefaultNetwork}. + * {@link ConnectivityManager#bindProcessToNetwork}. */ public class Network implements Parcelable { @@ -245,7 +245,10 @@ public class Network implements Parcelable { * @see java.net.URL#openConnection() */ public URLConnection openConnection(URL url) throws IOException { - final ConnectivityManager cm = ConnectivityManager.getInstance(); + final ConnectivityManager cm = ConnectivityManager.getInstanceOrNull(); + if (cm == null) { + throw new IOException("No ConnectivityManager yet constructed, please construct one"); + } // TODO: Should this be optimized to avoid fetching the global proxy for every request? ProxyInfo proxyInfo = cm.getGlobalProxy(); if (proxyInfo == null) { @@ -299,7 +302,7 @@ public class Network implements Parcelable { /** * Binds the specified {@link DatagramSocket} to this {@code Network}. All data traffic on the * socket will be sent on this {@code Network}, irrespective of any process-wide network binding - * set by {@link ConnectivityManager#setProcessDefaultNetwork}. The socket must not be + * set by {@link ConnectivityManager#bindProcessToNetwork}. The socket must not be * connected. */ public void bindSocket(DatagramSocket socket) throws IOException { @@ -316,7 +319,7 @@ public class Network implements Parcelable { /** * Binds the specified {@link Socket} to this {@code Network}. All data traffic on the socket * will be sent on this {@code Network}, irrespective of any process-wide network binding set by - * {@link ConnectivityManager#setProcessDefaultNetwork}. The socket must not be connected. + * {@link ConnectivityManager#bindProcessToNetwork}. The socket must not be connected. */ public void bindSocket(Socket socket) throws IOException { // Apparently, the kernel doesn't update a connected TCP socket's routing upon mark changes. diff --git a/core/java/android/net/NetworkUtils.java b/core/java/android/net/NetworkUtils.java index 02fbe7350b..29dd8ad9e9 100644 --- a/core/java/android/net/NetworkUtils.java +++ b/core/java/android/net/NetworkUtils.java @@ -159,7 +159,7 @@ public class NetworkUtils { * Return the netId last passed to {@link #bindProcessToNetwork}, or NETID_UNSET if * {@link #unbindProcessToNetwork} has been called since {@link #bindProcessToNetwork}. */ - public native static int getNetworkBoundToProcess(); + public native static int getBoundNetworkForProcess(); /** * Binds host resolutions performed by this process to the network designated by {@code netId}. diff --git a/core/jni/android_net_NetUtils.cpp b/core/jni/android_net_NetUtils.cpp index e97d61e54c..52da137f5c 100644 --- a/core/jni/android_net_NetUtils.cpp +++ b/core/jni/android_net_NetUtils.cpp @@ -268,7 +268,7 @@ static jboolean android_net_utils_bindProcessToNetwork(JNIEnv *env, jobject thiz return (jboolean) !setNetworkForProcess(netId); } -static jint android_net_utils_getNetworkBoundToProcess(JNIEnv *env, jobject thiz) +static jint android_net_utils_getBoundNetworkForProcess(JNIEnv *env, jobject thiz) { return getNetworkForProcess(); } @@ -306,7 +306,7 @@ static JNINativeMethod gNetworkUtilMethods[] = { { "releaseDhcpLease", "(Ljava/lang/String;)Z", (void *)android_net_utils_releaseDhcpLease }, { "getDhcpError", "()Ljava/lang/String;", (void*) android_net_utils_getDhcpError }, { "bindProcessToNetwork", "(I)Z", (void*) android_net_utils_bindProcessToNetwork }, - { "getNetworkBoundToProcess", "()I", (void*) android_net_utils_getNetworkBoundToProcess }, + { "getBoundNetworkForProcess", "()I", (void*) android_net_utils_getBoundNetworkForProcess }, { "bindProcessToNetworkForHostResolution", "(I)Z", (void*) android_net_utils_bindProcessToNetworkForHostResolution }, { "bindSocketToNetwork", "(II)I", (void*) android_net_utils_bindSocketToNetwork }, { "protectFromVpn", "(I)Z", (void*)android_net_utils_protectFromVpn }, From 6fabfa0e323cacfec45922de6813ab7ef40db929 Mon Sep 17 00:00:00 2001 From: Paul Jensen Date: Wed, 11 Mar 2015 11:51:46 -0400 Subject: [PATCH 2/4] Deprecate EXTRA_PROXY_INFO and unhide ConnectivityManager.getDefaultProxy(). 1. Unhide ConnectivityManager.getDefaultProxy() and update it to take into account process-bound-Networks. 2. Deprecate EXTRA_PROXY_INFO and instead encourage querying via getDefaultProxy(). Bug: 17905627 Bug: 17420465 Bug: 18144582 Change-Id: I45358ee82fe705d048022c8238b2452f52c37b88 --- core/java/android/net/ConnectivityManager.java | 1 - 1 file changed, 1 deletion(-) diff --git a/core/java/android/net/ConnectivityManager.java b/core/java/android/net/ConnectivityManager.java index 767113e848..099f68dfe3 100644 --- a/core/java/android/net/ConnectivityManager.java +++ b/core/java/android/net/ConnectivityManager.java @@ -1783,7 +1783,6 @@ public class ConnectivityManager { * * @return the {@link ProxyInfo} for the current HTTP proxy, or {@code null} if no * HTTP proxy is active. - * @hide */ public ProxyInfo getDefaultProxy() { final Network network = getBoundNetworkForProcess(); From bd2d378b7f35ae317b95fafcf8f25ea1876e11ed Mon Sep 17 00:00:00 2001 From: Paul Jensen Date: Fri, 13 Feb 2015 14:18:39 -0500 Subject: [PATCH 3/4] Fix typos in ConnectivityManager documentation. Spun off from https://partner-android-review.googlesource.com/#/c/205463/ for ease of review. Bug: 19416463 Change-Id: I2f88ac415177ecd96c9191df5e390830869f52e1 --- .../java/android/net/ConnectivityManager.java | 54 +++++++++---------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/core/java/android/net/ConnectivityManager.java b/core/java/android/net/ConnectivityManager.java index 099f68dfe3..317e2361f6 100644 --- a/core/java/android/net/ConnectivityManager.java +++ b/core/java/android/net/ConnectivityManager.java @@ -588,9 +588,9 @@ public class ConnectivityManager { * network. * * @return a {@link NetworkInfo} object for the current default network - * or {@code null} if no network default network is currently active + * or {@code null} if no default network is currently active * - *

This method requires the call to hold the permission + *

This method requires the caller to hold the permission * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. */ public NetworkInfo getActiveNetworkInfo() { @@ -737,9 +737,9 @@ public class ConnectivityManager { * network. * * @return a {@link NetworkInfo} object for the current default network - * or {@code null} if no network default network is currently active + * or {@code null} if no default network is currently active * - *

This method requires the call to hold the permission + *

This method requires the caller to hold the permission * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. * * {@hide} @@ -759,7 +759,7 @@ public class ConnectivityManager { * for the current default network, or {@code null} if there * is no current default network. * - *

This method requires the call to hold the permission + *

This method requires the caller to hold the permission * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. * {@hide} */ @@ -779,7 +779,7 @@ public class ConnectivityManager { * for the given networkType, or {@code null} if there is * no current default network. * - *

This method requires the call to hold the permission + *

This method requires the caller to hold the permission * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. * {@hide} */ @@ -829,7 +829,7 @@ public class ConnectivityManager { * @return a boolean, {@code true} indicating success. All network types * will be tried, even if some fail. * - *

This method requires the call to hold the permission + *

This method requires the caller to hold the permission * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}. * {@hide} */ @@ -850,7 +850,7 @@ public class ConnectivityManager { * {@code} false to turn it off. * @return a boolean, {@code true} indicating success. * - *

This method requires the call to hold the permission + *

This method requires the caller to hold the permission * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}. * {@hide} */ @@ -1272,7 +1272,7 @@ public class ConnectivityManager { * network is active. Quota status can change rapidly, so these values * shouldn't be cached. * - *

This method requires the call to hold the permission + *

This method requires the caller to hold the permission * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. * * @hide @@ -1451,7 +1451,7 @@ public class ConnectivityManager { * * @return an array of 0 or more Strings of tetherable interface names. * - *

This method requires the call to hold the permission + *

This method requires the caller to hold the permission * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. * {@hide} */ @@ -1468,7 +1468,7 @@ public class ConnectivityManager { * * @return an array of 0 or more String of currently tethered interface names. * - *

This method requires the call to hold the permission + *

This method requires the caller to hold the permission * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. * {@hide} */ @@ -1491,7 +1491,7 @@ public class ConnectivityManager { * @return an array of 0 or more String indicating the interface names * which failed to tether. * - *

This method requires the call to hold the permission + *

This method requires the caller to hold the permission * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. * {@hide} */ @@ -1529,7 +1529,7 @@ public class ConnectivityManager { * @param iface the interface name to tether. * @return error a {@code TETHER_ERROR} value indicating success or failure type * - *

This method requires the call to hold the permission + *

This method requires the caller to hold the permission * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}. * {@hide} */ @@ -1547,7 +1547,7 @@ public class ConnectivityManager { * @param iface the interface name to untether. * @return error a {@code TETHER_ERROR} value indicating success or failure type * - *

This method requires the call to hold the permission + *

This method requires the caller to hold the permission * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}. * {@hide} */ @@ -1566,7 +1566,7 @@ public class ConnectivityManager { * * @return a boolean - {@code true} indicating Tethering is supported. * - *

This method requires the call to hold the permission + *

This method requires the caller to hold the permission * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. * {@hide} */ @@ -1586,7 +1586,7 @@ public class ConnectivityManager { * @return an array of 0 or more regular expression Strings defining * what interfaces are considered tetherable usb interfaces. * - *

This method requires the call to hold the permission + *

This method requires the caller to hold the permission * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. * {@hide} */ @@ -1606,7 +1606,7 @@ public class ConnectivityManager { * @return an array of 0 or more regular expression Strings defining * what interfaces are considered tetherable wifi interfaces. * - *

This method requires the call to hold the permission + *

This method requires the caller to hold the permission * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. * {@hide} */ @@ -1626,7 +1626,7 @@ public class ConnectivityManager { * @return an array of 0 or more regular expression Strings defining * what interfaces are considered tetherable bluetooth interfaces. * - *

This method requires the call to hold the permission + *

This method requires the caller to hold the permission * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. * {@hide} */ @@ -1648,7 +1648,7 @@ public class ConnectivityManager { * @param enable a boolean - {@code true} to enable tethering * @return error a {@code TETHER_ERROR} value indicating success or failure type * - *

This method requires the call to hold the permission + *

This method requires the caller to hold the permission * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}. * {@hide} */ @@ -1691,7 +1691,7 @@ public class ConnectivityManager { * @return error The error code of the last error tethering or untethering the named * interface * - *

This method requires the call to hold the permission + *

This method requires the caller to hold the permission * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. * {@hide} */ @@ -1710,7 +1710,7 @@ public class ConnectivityManager { * @param networkType The type of network you want to report on * @param percentage The quality of the connection 0 is bad, 100 is good * - *

This method requires the call to hold the permission + *

This method requires the caller to hold the permission * {@link android.Manifest.permission#STATUS_BAR}. * {@hide} */ @@ -1746,7 +1746,7 @@ public class ConnectivityManager { * @param p The a {@link ProxyInfo} object defining the new global * HTTP proxy. A {@code null} value will clear the global HTTP proxy. * - *

This method requires the call to hold the permission + *

This method requires the caller to hold the permission * android.Manifest.permission#CONNECTIVITY_INTERNAL. * @hide */ @@ -1763,7 +1763,7 @@ public class ConnectivityManager { * @return {@link ProxyInfo} for the current global HTTP proxy or {@code null} * if no global HTTP proxy is set. * - *

This method requires the call to hold the permission + *

This method requires the caller to hold the permission * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. * @hide */ @@ -1811,7 +1811,7 @@ public class ConnectivityManager { * @param networkType The network type we'd like to check * @return {@code true} if supported, else {@code false} * - *

This method requires the call to hold the permission + *

This method requires the caller to hold the permission * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. * @hide */ @@ -1833,7 +1833,7 @@ public class ConnectivityManager { * @return {@code true} if large transfers should be avoided, otherwise * {@code false}. * - *

This method requires the call to hold the permission + *

This method requires the caller to hold the permission * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. */ public boolean isActiveNetworkMetered() { @@ -1869,7 +1869,7 @@ public class ConnectivityManager { * in question. * @param isCaptivePortal true/false. * - *

This method requires the call to hold the permission + *

This method requires the caller to hold the permission * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}. * {@hide} */ @@ -1944,7 +1944,7 @@ public class ConnectivityManager { * * @param enable whether to enable airplane mode or not * - *

This method requires the call to hold the permission + *

This method requires the caller to hold the permission * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}. * @hide */ From 224ab3d3a3edb441f2e609cc39ef2065d3c78069 Mon Sep 17 00:00:00 2001 From: Lorenzo Colitti Date: Tue, 7 Apr 2015 12:58:52 +0900 Subject: [PATCH 4/4] Unhide Network.openConnection(URL, Proxy). Spun off from https://partner-android-review.googlesource.com/#/c/205463/ for ease of review. Bug: 19416463 Change-Id: Ia37a53a350d55b5514b949242368dfe396f6b9a0 --- core/java/android/net/Network.java | 1 - 1 file changed, 1 deletion(-) diff --git a/core/java/android/net/Network.java b/core/java/android/net/Network.java index df6eb99d46..39db41d1f2 100644 --- a/core/java/android/net/Network.java +++ b/core/java/android/net/Network.java @@ -275,7 +275,6 @@ public class Network implements Parcelable { * @throws IllegalArgumentException if the argument proxy is null. * @throws IOException if an error occurs while opening the connection. * @see java.net.URL#openConnection() - * @hide */ public URLConnection openConnection(URL url, java.net.Proxy proxy) throws IOException { if (proxy == null) throw new IllegalArgumentException("proxy is null");