From 0eb55c16c4bce94c9b714783e1168c8898392a7b Mon Sep 17 00:00:00 2001 From: Robert Greenwalt Date: Sun, 18 May 2014 16:22:10 -0700 Subject: [PATCH] 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 --- core/java/android/net/NetworkRequest.java | 33 ++++++++----------- .../android/server/ConnectivityService.java | 14 ++++++-- 2 files changed, 25 insertions(+), 22 deletions(-) diff --git a/core/java/android/net/NetworkRequest.java b/core/java/android/net/NetworkRequest.java index b3ae3f51e8..80074a5e7a 100644 --- a/core/java/android/net/NetworkRequest.java +++ b/core/java/android/net/NetworkRequest.java @@ -22,11 +22,19 @@ import android.os.Parcelable; 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 */ 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; @@ -34,7 +42,7 @@ public class NetworkRequest implements Parcelable { * Identifies the request. NetworkRequests should only be constructed by * the Framework and given out to applications as tokens to be used to identify * the request. - * TODO - make sure this input is checked whenever a NR is passed in a public API + * @hide */ public final int requestId; @@ -45,31 +53,18 @@ public class NetworkRequest implements Parcelable { */ public final boolean needsBroadcasts; - private static final AtomicInteger sNextRequestId = new AtomicInteger(1); - /** * @hide */ - public NetworkRequest(NetworkCapabilities nc) { - 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) { + public NetworkRequest(NetworkCapabilities nc, boolean needsBroadcasts, int rId) { requestId = rId; networkCapabilities = nc; this.needsBroadcasts = needsBroadcasts; } + /** + * @hide + */ public NetworkRequest(NetworkRequest that) { networkCapabilities = new NetworkCapabilities(that.networkCapabilities); requestId = that.requestId; diff --git a/services/core/java/com/android/server/ConnectivityService.java b/services/core/java/com/android/server/ConnectivityService.java index 5f53e4946d..01af753b90 100644 --- a/services/core/java/com/android/server/ConnectivityService.java +++ b/services/core/java/com/android/server/ConnectivityService.java @@ -507,10 +507,14 @@ public class ConnectivityService extends IConnectivityManager.Stub { TelephonyManager mTelephonyManager; + // sequence number for Networks private final static int MIN_NET_ID = 10; // some reserved marks private final static int MAX_NET_ID = 65535; private int mNextNetId = MIN_NET_ID; + // sequence number of NetworkRequests + private int mNextNetworkRequestId = 1; + public ConnectivityService(Context context, INetworkManagementService netd, INetworkStatsService statsService, INetworkPolicyManager policyManager) { // Currently, omitting a NetworkFactory will create one internally @@ -526,7 +530,7 @@ public class ConnectivityService extends IConnectivityManager.Stub { NetworkCapabilities netCap = new NetworkCapabilities(); netCap.addNetworkCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET); 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.REQUEST); mNetworkRequests.put(mDefaultRequest, nri); @@ -773,6 +777,10 @@ public class ConnectivityService extends IConnectivityManager.Stub { mAppOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE); } + private synchronized int nextNetworkRequestId() { + return mNextNetworkRequestId++; + } + private synchronized int nextNetId() { int netId = mNextNetId; 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"); } NetworkRequest networkRequest = new NetworkRequest(new NetworkCapabilities( - networkCapabilities)); + networkCapabilities), false, nextNetworkRequestId()); if (DBG) log("requestNetwork for " + networkRequest); NetworkRequestInfo nri = new NetworkRequestInfo(messenger, networkRequest, binder, NetworkRequestInfo.REQUEST); @@ -5297,7 +5305,7 @@ public class ConnectivityService extends IConnectivityManager.Stub { enforceAccessPermission(); NetworkRequest networkRequest = new NetworkRequest(new NetworkCapabilities( - networkCapabilities)); + networkCapabilities), false, nextNetworkRequestId()); if (DBG) log("listenForNetwork for " + networkRequest); NetworkRequestInfo nri = new NetworkRequestInfo(messenger, networkRequest, binder, NetworkRequestInfo.LISTEN);