Add javadoc for NetworkRequest.

Also moved the requestId serial number out of this public class into CS.

Had to leave NetworkRequest hidden for now because the docs refer to things still hidden
in ConnectivityManager.

Change-Id: I14d1fe52d992adf5e4dc197b8f5433e40b0adfe6
This commit is contained in:
Robert Greenwalt
2014-05-18 16:22:10 -07:00
parent 05d3bc2fd5
commit 0eb55c16c4
2 changed files with 25 additions and 22 deletions

View File

@@ -22,11 +22,19 @@ import android.os.Parcelable;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
/** /**
* Defines a request for a network, made by calling {@link ConnectivityManager.requestNetwork}.
*
* This token records the {@link NetworkCapabilities} used to make the request and identifies
* the request. It should be used to release the request via
* {@link ConnectivityManager.releaseNetworkRequest} when the network is no longer desired.
* @hide * @hide
*/ */
public class NetworkRequest implements Parcelable { public class NetworkRequest implements Parcelable {
/** /**
* The NetworkCapabilities that define this request * The {@link NetworkCapabilities} that define this request. This should not be modified.
* The networkCapabilities of the request are set when
* {@link ConnectivityManager.requestNetwork} is called and the value is presented here
* as a convenient reminder of what was requested.
*/ */
public final NetworkCapabilities networkCapabilities; public final NetworkCapabilities networkCapabilities;
@@ -34,7 +42,7 @@ public class NetworkRequest implements Parcelable {
* Identifies the request. NetworkRequests should only be constructed by * Identifies the request. NetworkRequests should only be constructed by
* the Framework and given out to applications as tokens to be used to identify * the Framework and given out to applications as tokens to be used to identify
* the request. * the request.
* TODO - make sure this input is checked whenever a NR is passed in a public API * @hide
*/ */
public final int requestId; public final int requestId;
@@ -45,31 +53,18 @@ public class NetworkRequest implements Parcelable {
*/ */
public final boolean needsBroadcasts; public final boolean needsBroadcasts;
private static final AtomicInteger sNextRequestId = new AtomicInteger(1);
/** /**
* @hide * @hide
*/ */
public NetworkRequest(NetworkCapabilities nc) { public NetworkRequest(NetworkCapabilities nc, boolean needsBroadcasts, int rId) {
this(nc, false, sNextRequestId.getAndIncrement());
}
/**
* @hide
*/
public NetworkRequest(NetworkCapabilities nc, boolean needsBroadcasts) {
this(nc, needsBroadcasts, sNextRequestId.getAndIncrement());
}
/**
* @hide
*/
private NetworkRequest(NetworkCapabilities nc, boolean needsBroadcasts, int rId) {
requestId = rId; requestId = rId;
networkCapabilities = nc; networkCapabilities = nc;
this.needsBroadcasts = needsBroadcasts; this.needsBroadcasts = needsBroadcasts;
} }
/**
* @hide
*/
public NetworkRequest(NetworkRequest that) { public NetworkRequest(NetworkRequest that) {
networkCapabilities = new NetworkCapabilities(that.networkCapabilities); networkCapabilities = new NetworkCapabilities(that.networkCapabilities);
requestId = that.requestId; requestId = that.requestId;

View File

@@ -507,10 +507,14 @@ public class ConnectivityService extends IConnectivityManager.Stub {
TelephonyManager mTelephonyManager; TelephonyManager mTelephonyManager;
// sequence number for Networks
private final static int MIN_NET_ID = 10; // some reserved marks private final static int MIN_NET_ID = 10; // some reserved marks
private final static int MAX_NET_ID = 65535; private final static int MAX_NET_ID = 65535;
private int mNextNetId = MIN_NET_ID; private int mNextNetId = MIN_NET_ID;
// sequence number of NetworkRequests
private int mNextNetworkRequestId = 1;
public ConnectivityService(Context context, INetworkManagementService netd, public ConnectivityService(Context context, INetworkManagementService netd,
INetworkStatsService statsService, INetworkPolicyManager policyManager) { INetworkStatsService statsService, INetworkPolicyManager policyManager) {
// Currently, omitting a NetworkFactory will create one internally // Currently, omitting a NetworkFactory will create one internally
@@ -526,7 +530,7 @@ public class ConnectivityService extends IConnectivityManager.Stub {
NetworkCapabilities netCap = new NetworkCapabilities(); NetworkCapabilities netCap = new NetworkCapabilities();
netCap.addNetworkCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET); netCap.addNetworkCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
netCap.addNetworkCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED); netCap.addNetworkCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED);
mDefaultRequest = new NetworkRequest(netCap, true); mDefaultRequest = new NetworkRequest(netCap, true, nextNetworkRequestId());
NetworkRequestInfo nri = new NetworkRequestInfo(null, mDefaultRequest, new Binder(), NetworkRequestInfo nri = new NetworkRequestInfo(null, mDefaultRequest, new Binder(),
NetworkRequestInfo.REQUEST); NetworkRequestInfo.REQUEST);
mNetworkRequests.put(mDefaultRequest, nri); mNetworkRequests.put(mDefaultRequest, nri);
@@ -773,6 +777,10 @@ public class ConnectivityService extends IConnectivityManager.Stub {
mAppOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE); mAppOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
} }
private synchronized int nextNetworkRequestId() {
return mNextNetworkRequestId++;
}
private synchronized int nextNetId() { private synchronized int nextNetId() {
int netId = mNextNetId; int netId = mNextNetId;
if (++mNextNetId > MAX_NET_ID) mNextNetId = MIN_NET_ID; if (++mNextNetId > MAX_NET_ID) mNextNetId = MIN_NET_ID;
@@ -5271,7 +5279,7 @@ public class ConnectivityService extends IConnectivityManager.Stub {
throw new IllegalArgumentException("Bad timeout specified"); throw new IllegalArgumentException("Bad timeout specified");
} }
NetworkRequest networkRequest = new NetworkRequest(new NetworkCapabilities( NetworkRequest networkRequest = new NetworkRequest(new NetworkCapabilities(
networkCapabilities)); networkCapabilities), false, nextNetworkRequestId());
if (DBG) log("requestNetwork for " + networkRequest); if (DBG) log("requestNetwork for " + networkRequest);
NetworkRequestInfo nri = new NetworkRequestInfo(messenger, networkRequest, binder, NetworkRequestInfo nri = new NetworkRequestInfo(messenger, networkRequest, binder,
NetworkRequestInfo.REQUEST); NetworkRequestInfo.REQUEST);
@@ -5297,7 +5305,7 @@ public class ConnectivityService extends IConnectivityManager.Stub {
enforceAccessPermission(); enforceAccessPermission();
NetworkRequest networkRequest = new NetworkRequest(new NetworkCapabilities( NetworkRequest networkRequest = new NetworkRequest(new NetworkCapabilities(
networkCapabilities)); networkCapabilities), false, nextNetworkRequestId());
if (DBG) log("listenForNetwork for " + networkRequest); if (DBG) log("listenForNetwork for " + networkRequest);
NetworkRequestInfo nri = new NetworkRequestInfo(messenger, networkRequest, binder, NetworkRequestInfo nri = new NetworkRequestInfo(messenger, networkRequest, binder,
NetworkRequestInfo.LISTEN); NetworkRequestInfo.LISTEN);