summaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorRobert Greenwalt <rgreenwalt@google.com>2014-06-08 23:41:27 +0000
committerAndroid (Google) Code Review <android-gerrit@google.com>2014-06-08 23:41:27 +0000
commitcc62d9ad6bb84f2d5ccf8c4b64f581990bf43751 (patch)
treee848e9bd0e4717111a5aa2edfca97d98b9291649 /core
parentdf2b878ff4e7b4a258588d3a93574c399db78a07 (diff)
parent6d3ff9ea3a542a18ca5da055418a7b9eff130d93 (diff)
downloadframeworks_base-cc62d9ad6bb84f2d5ccf8c4b64f581990bf43751.zip
frameworks_base-cc62d9ad6bb84f2d5ccf8c4b64f581990bf43751.tar.gz
frameworks_base-cc62d9ad6bb84f2d5ccf8c4b64f581990bf43751.tar.bz2
Merge "Apply API review to android.net.Network:"
Diffstat (limited to 'core')
-rw-r--r--core/java/android/net/ConnectivityManager.java69
-rw-r--r--core/java/android/net/Network.java55
-rw-r--r--core/java/android/net/NetworkUtils.java2
3 files changed, 69 insertions, 57 deletions
diff --git a/core/java/android/net/ConnectivityManager.java b/core/java/android/net/ConnectivityManager.java
index 59732ee..27402668 100644
--- a/core/java/android/net/ConnectivityManager.java
+++ b/core/java/android/net/ConnectivityManager.java
@@ -22,6 +22,7 @@ import android.annotation.SdkConstant.SdkConstantType;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
+import android.net.NetworkUtils;
import android.os.Binder;
import android.os.Build.VERSION_CODES;
import android.os.Handler;
@@ -982,13 +983,13 @@ public class ConnectivityManager {
public void onAvailable(NetworkRequest request, Network network) {
currentNetwork = network;
Log.d(TAG, "startUsingNetworkFeature got Network:" + network);
- network.bindProcessForHostResolution();
+ setProcessDefaultNetworkForHostResolution(network);
}
@Override
public void onLost(NetworkRequest request, Network network) {
if (network.equals(currentNetwork)) {
currentNetwork = null;
- network.unbindProcessForHostResolution();
+ setProcessDefaultNetworkForHostResolution(null);
}
Log.d(TAG, "startUsingNetworkFeature lost Network:" + network);
}
@@ -1072,7 +1073,7 @@ public class ConnectivityManager {
* @return {@code true} on success, {@code false} on failure
*
* @deprecated Deprecated in favor of the {@link #requestNetwork},
- * {@link Network#bindProcess} and {@link Network#socketFactory} api.
+ * {@link #setProcessDefaultNetwork} and {@link Network#getSocketFactory} api.
*/
public boolean requestRouteToHost(int networkType, int hostAddress) {
InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress);
@@ -1096,7 +1097,7 @@ public class ConnectivityManager {
* @return {@code true} on success, {@code false} on failure
* @hide
* @deprecated Deprecated in favor of the {@link #requestNetwork} and
- * {@link Network#bindProcess} api.
+ * {@link #setProcessDefaultNetwork} api.
*/
public boolean requestRouteToHostAddress(int networkType, InetAddress hostAddress) {
byte[] address = hostAddress.getAddress();
@@ -2348,4 +2349,64 @@ public class ConnectivityManager {
mService.releaseNetworkRequest(networkRequest);
} catch (RemoteException e) {}
}
+
+ /**
+ * 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.
+ */
+ public static boolean setProcessDefaultNetwork(Network network) {
+ if (network == null) {
+ NetworkUtils.unbindProcessToNetwork();
+ } else {
+ NetworkUtils.bindProcessToNetwork(network.netId);
+ }
+ // TODO fix return value
+ return true;
+ }
+
+ /**
+ * Returns the {@link Network} currently bound to this process via
+ * {@link #setProcessDefaultNetwork}, or {@code null} if no {@link Network} is explicitly bound.
+ *
+ * @return {@code Network} to which this process is bound, or {@code null}.
+ */
+ public static Network getProcessDefaultNetwork() {
+ int netId = NetworkUtils.getNetworkBoundToProcess();
+ if (netId == 0) return null;
+ return new Network(netId);
+ }
+
+ /**
+ * Binds host resolutions performed by this process to {@code network}.
+ * {@link #setProcessDefaultNetwork} 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.
+ * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
+ * @hide
+ * @deprecated This is strictly for legacy usage to support {@link #startUsingNetworkFeature}.
+ */
+ public static boolean setProcessDefaultNetworkForHostResolution(Network network) {
+ if (network == null) {
+ NetworkUtils.unbindProcessToNetworkForHostResolution();
+ } else {
+ NetworkUtils.bindProcessToNetworkForHostResolution(network.netId);
+ }
+ // TODO hook up the return value.
+ return true;
+ }
}
diff --git a/core/java/android/net/Network.java b/core/java/android/net/Network.java
index 64516e6..d933f26 100644
--- a/core/java/android/net/Network.java
+++ b/core/java/android/net/Network.java
@@ -32,7 +32,8 @@ import javax.net.SocketFactory;
* {@link ConnectivityManager.NetworkCallbackListener} in response to
* {@link ConnectivityManager#requestNetwork} or {@link ConnectivityManager#listenForNetwork}.
* 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 #bindProcess}.
+ * through a targeted {@link SocketFactory} or process-wide via
+ * {@link ConnectivityManager#setProcessDefaultNetwork}.
*/
public class Network implements Parcelable {
@@ -160,63 +161,13 @@ public class Network implements Parcelable {
* @return a {@link SocketFactory} which produces {@link Socket} instances bound to this
* {@code Network}.
*/
- public SocketFactory socketFactory() {
+ public SocketFactory getSocketFactory() {
if (mNetworkBoundSocketFactory == null) {
mNetworkBoundSocketFactory = new NetworkBoundSocketFactory(netId);
}
return mNetworkBoundSocketFactory;
}
- /**
- * Binds the current process to this network. All sockets created in the future (and not
- * explicitly bound via a bound {@link SocketFactory} (see {@link Network#socketFactory})
- * will be bound to this network. Note that if this {@code Network} ever disconnects
- * all sockets created in this way will cease to work. This is by design so an application
- * doesn't accidentally use sockets it thinks are still bound to a particular {@code Network}.
- */
- public void bindProcess() {
- NetworkUtils.bindProcessToNetwork(netId);
- }
-
- /**
- * Binds host resolutions performed by this process to this network. {@link #bindProcess}
- * takes precedence over this setting.
- *
- * @hide
- * @deprecated This is strictly for legacy usage to support startUsingNetworkFeature().
- */
- public void bindProcessForHostResolution() {
- NetworkUtils.bindProcessToNetworkForHostResolution(netId);
- }
-
- /**
- * Clears any process specific {@link Network} binding for host resolution. This does
- * not clear bindings enacted via {@link #bindProcess}.
- *
- * @hide
- * @deprecated This is strictly for legacy usage to support startUsingNetworkFeature().
- */
- public void unbindProcessForHostResolution() {
- NetworkUtils.unbindProcessToNetworkForHostResolution();
- }
-
- /**
- * A static utility method to return any {@code Network} currently bound by this process.
- *
- * @return {@code Network} to which this process is bound.
- */
- public static Network getProcessBoundNetwork() {
- return new Network(NetworkUtils.getNetworkBoundToProcess());
- }
-
- /**
- * Clear any process specific {@code Network} binding. This reverts a call to
- * {@link Network#bindProcess}.
- */
- public static void unbindProcess() {
- NetworkUtils.unbindProcessToNetwork();
- }
-
// implement the Parcelable interface
public int describeContents() {
return 0;
diff --git a/core/java/android/net/NetworkUtils.java b/core/java/android/net/NetworkUtils.java
index edb3286..b02f88e 100644
--- a/core/java/android/net/NetworkUtils.java
+++ b/core/java/android/net/NetworkUtils.java
@@ -111,7 +111,7 @@ public class NetworkUtils {
/**
* Binds the current process to the network designated by {@code netId}. All sockets created
* in the future (and not explicitly bound via a bound {@link SocketFactory} (see
- * {@link Network#socketFactory}) will be bound to this network. Note that if this
+ * {@link Network#getSocketFactory}) will be bound to this network. Note that if this
* {@code Network} ever disconnects all sockets created in this way will cease to work. This
* is by design so an application doesn't accidentally use sockets it thinks are still bound to
* a particular {@code Network}.