Merge "Apply API review to android.net.Network:" into lmp-preview-dev

This commit is contained in:
Robert Greenwalt
2014-06-10 23:30:34 +00:00
committed by Android (Google) Code Review
4 changed files with 72 additions and 61 deletions

View File

@@ -15955,6 +15955,7 @@ package android.net {
method public android.net.NetworkCapabilities getNetworkCapabilities(android.net.Network);
method public android.net.NetworkInfo getNetworkInfo(int);
method public deprecated int getNetworkPreference();
method public static android.net.Network getProcessDefaultNetwork();
method public boolean isActiveNetworkMetered();
method public boolean isNetworkActive();
method public static boolean isNetworkTypeValid(int);
@@ -15966,6 +15967,7 @@ package android.net {
method public android.net.NetworkRequest requestNetwork(android.net.NetworkCapabilities, android.app.PendingIntent);
method public deprecated boolean requestRouteToHost(int, int);
method public deprecated void setNetworkPreference(int);
method public static boolean setProcessDefaultNetwork(android.net.Network);
method public deprecated int startUsingNetworkFeature(int, java.lang.String);
method public deprecated int stopUsingNetworkFeature(int, java.lang.String);
method public void unregisterNetworkActiveListener(android.net.ConnectivityManager.OnNetworkActiveListener);
@@ -16138,13 +16140,10 @@ package android.net {
}
public class Network implements android.os.Parcelable {
method public void bindProcess();
method public int describeContents();
method public java.net.InetAddress[] getAllByName(java.lang.String) throws java.net.UnknownHostException;
method public java.net.InetAddress getByName(java.lang.String) throws java.net.UnknownHostException;
method public static android.net.Network getProcessBoundNetwork();
method public javax.net.SocketFactory socketFactory();
method public static void unbindProcess();
method public javax.net.SocketFactory getSocketFactory();
method public void writeToParcel(android.os.Parcel, int);
field public static final android.os.Parcelable.Creator CREATOR;
}

View File

@@ -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;
@@ -981,13 +982,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);
}
@@ -1071,7 +1072,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);
@@ -1095,7 +1096,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();
@@ -2347,4 +2348,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;
}
}

View File

@@ -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;

View File

@@ -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}.