Merge "Move NetworkAgent#register to a new method." am: 9309eea2fe
Change-Id: I4ca48f4232ad4733da5ce7f265fc911f5ff1a713
This commit is contained in:
@@ -4611,6 +4611,7 @@ package android.net {
|
|||||||
|
|
||||||
public abstract class NetworkAgent {
|
public abstract class NetworkAgent {
|
||||||
ctor public NetworkAgent(@NonNull android.content.Context, @NonNull android.os.Looper, @NonNull String, @NonNull android.net.NetworkCapabilities, @NonNull android.net.LinkProperties, int, @NonNull android.net.NetworkAgentConfig, @Nullable android.net.NetworkProvider);
|
ctor public NetworkAgent(@NonNull android.content.Context, @NonNull android.os.Looper, @NonNull String, @NonNull android.net.NetworkCapabilities, @NonNull android.net.LinkProperties, int, @NonNull android.net.NetworkAgentConfig, @Nullable android.net.NetworkProvider);
|
||||||
|
method @Nullable public android.net.Network getNetwork();
|
||||||
method public void onAddKeepalivePacketFilter(int, @NonNull android.net.KeepalivePacketData);
|
method public void onAddKeepalivePacketFilter(int, @NonNull android.net.KeepalivePacketData);
|
||||||
method public void onAutomaticReconnectDisabled();
|
method public void onAutomaticReconnectDisabled();
|
||||||
method public void onBandwidthUpdateRequested();
|
method public void onBandwidthUpdateRequested();
|
||||||
@@ -4621,6 +4622,7 @@ package android.net {
|
|||||||
method public void onStartSocketKeepalive(int, int, @NonNull android.net.KeepalivePacketData);
|
method public void onStartSocketKeepalive(int, int, @NonNull android.net.KeepalivePacketData);
|
||||||
method public void onStopSocketKeepalive(int);
|
method public void onStopSocketKeepalive(int);
|
||||||
method public void onValidationStatus(int, @Nullable String);
|
method public void onValidationStatus(int, @Nullable String);
|
||||||
|
method @NonNull public android.net.Network register();
|
||||||
method public void sendLinkProperties(@NonNull android.net.LinkProperties);
|
method public void sendLinkProperties(@NonNull android.net.LinkProperties);
|
||||||
method public void sendNetworkCapabilities(@NonNull android.net.NetworkCapabilities);
|
method public void sendNetworkCapabilities(@NonNull android.net.NetworkCapabilities);
|
||||||
method public void sendNetworkScore(int);
|
method public void sendNetworkScore(int);
|
||||||
@@ -4631,7 +4633,6 @@ package android.net {
|
|||||||
method public void unregister();
|
method public void unregister();
|
||||||
field public static final int VALIDATION_STATUS_NOT_VALID = 2; // 0x2
|
field public static final int VALIDATION_STATUS_NOT_VALID = 2; // 0x2
|
||||||
field public static final int VALIDATION_STATUS_VALID = 1; // 0x1
|
field public static final int VALIDATION_STATUS_VALID = 1; // 0x1
|
||||||
field @NonNull public final android.net.Network network;
|
|
||||||
field public final int providerId;
|
field public final int providerId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -51,8 +51,8 @@ public abstract class NetworkAgent {
|
|||||||
/**
|
/**
|
||||||
* The {@link Network} corresponding to this object.
|
* The {@link Network} corresponding to this object.
|
||||||
*/
|
*/
|
||||||
@NonNull
|
@Nullable
|
||||||
public final Network network;
|
private volatile Network mNetwork;
|
||||||
|
|
||||||
// Whether this NetworkAgent is using the legacy (never unhidden) API. The difference is
|
// Whether this NetworkAgent is using the legacy (never unhidden) API. The difference is
|
||||||
// that the legacy API uses NetworkInfo to convey the state, while the current API is
|
// that the legacy API uses NetworkInfo to convey the state, while the current API is
|
||||||
@@ -65,7 +65,6 @@ public abstract class NetworkAgent {
|
|||||||
private final String LOG_TAG;
|
private final String LOG_TAG;
|
||||||
private static final boolean DBG = true;
|
private static final boolean DBG = true;
|
||||||
private static final boolean VDBG = false;
|
private static final boolean VDBG = false;
|
||||||
private final Context mContext;
|
|
||||||
private final ArrayList<Message>mPreConnectedQueue = new ArrayList<Message>();
|
private final ArrayList<Message>mPreConnectedQueue = new ArrayList<Message>();
|
||||||
private volatile long mLastBwRefreshTime = 0;
|
private volatile long mLastBwRefreshTime = 0;
|
||||||
private static final long BW_REFRESH_MIN_WIN_MS = 500;
|
private static final long BW_REFRESH_MIN_WIN_MS = 500;
|
||||||
@@ -277,18 +276,21 @@ public abstract class NetworkAgent {
|
|||||||
public NetworkAgent(Looper looper, Context context, String logTag, NetworkInfo ni,
|
public NetworkAgent(Looper looper, Context context, String logTag, NetworkInfo ni,
|
||||||
NetworkCapabilities nc, LinkProperties lp, int score) {
|
NetworkCapabilities nc, LinkProperties lp, int score) {
|
||||||
this(looper, context, logTag, ni, nc, lp, score, null, NetworkProvider.ID_NONE);
|
this(looper, context, logTag, ni, nc, lp, score, null, NetworkProvider.ID_NONE);
|
||||||
|
// Register done by the constructor called in the previous line
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @hide TODO: remove and replace usage with the public constructor. */
|
/** @hide TODO: remove and replace usage with the public constructor. */
|
||||||
public NetworkAgent(Looper looper, Context context, String logTag, NetworkInfo ni,
|
public NetworkAgent(Looper looper, Context context, String logTag, NetworkInfo ni,
|
||||||
NetworkCapabilities nc, LinkProperties lp, int score, NetworkAgentConfig config) {
|
NetworkCapabilities nc, LinkProperties lp, int score, NetworkAgentConfig config) {
|
||||||
this(looper, context, logTag, ni, nc, lp, score, config, NetworkProvider.ID_NONE);
|
this(looper, context, logTag, ni, nc, lp, score, config, NetworkProvider.ID_NONE);
|
||||||
|
// Register done by the constructor called in the previous line
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @hide TODO: remove and replace usage with the public constructor. */
|
/** @hide TODO: remove and replace usage with the public constructor. */
|
||||||
public NetworkAgent(Looper looper, Context context, String logTag, NetworkInfo ni,
|
public NetworkAgent(Looper looper, Context context, String logTag, NetworkInfo ni,
|
||||||
NetworkCapabilities nc, LinkProperties lp, int score, int providerId) {
|
NetworkCapabilities nc, LinkProperties lp, int score, int providerId) {
|
||||||
this(looper, context, logTag, ni, nc, lp, score, null, providerId);
|
this(looper, context, logTag, ni, nc, lp, score, null, providerId);
|
||||||
|
// Register done by the constructor called in the previous line
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @hide TODO: remove and replace usage with the public constructor. */
|
/** @hide TODO: remove and replace usage with the public constructor. */
|
||||||
@@ -296,6 +298,7 @@ public abstract class NetworkAgent {
|
|||||||
NetworkCapabilities nc, LinkProperties lp, int score, NetworkAgentConfig config,
|
NetworkCapabilities nc, LinkProperties lp, int score, NetworkAgentConfig config,
|
||||||
int providerId) {
|
int providerId) {
|
||||||
this(looper, context, logTag, nc, lp, score, config, providerId, ni, true /* legacy */);
|
this(looper, context, logTag, nc, lp, score, config, providerId, ni, true /* legacy */);
|
||||||
|
register();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static NetworkInfo getLegacyNetworkInfo(final NetworkAgentConfig config) {
|
private static NetworkInfo getLegacyNetworkInfo(final NetworkAgentConfig config) {
|
||||||
@@ -327,12 +330,32 @@ public abstract class NetworkAgent {
|
|||||||
getLegacyNetworkInfo(config), false /* legacy */);
|
getLegacyNetworkInfo(config), false /* legacy */);
|
||||||
}
|
}
|
||||||
|
|
||||||
private NetworkAgent(Looper looper, Context context, String logTag, NetworkCapabilities nc,
|
private static class InitialConfiguration {
|
||||||
LinkProperties lp, int score, NetworkAgentConfig config, int providerId,
|
public final Context context;
|
||||||
NetworkInfo ni, boolean legacy) {
|
public final NetworkCapabilities capabilities;
|
||||||
|
public final LinkProperties properties;
|
||||||
|
public final int score;
|
||||||
|
public final NetworkAgentConfig config;
|
||||||
|
public final NetworkInfo info;
|
||||||
|
InitialConfiguration(@NonNull Context context, @NonNull NetworkCapabilities capabilities,
|
||||||
|
@NonNull LinkProperties properties, int score, @NonNull NetworkAgentConfig config,
|
||||||
|
@NonNull NetworkInfo info) {
|
||||||
|
this.context = context;
|
||||||
|
this.capabilities = capabilities;
|
||||||
|
this.properties = properties;
|
||||||
|
this.score = score;
|
||||||
|
this.config = config;
|
||||||
|
this.info = info;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private volatile InitialConfiguration mInitialConfiguration;
|
||||||
|
|
||||||
|
private NetworkAgent(@NonNull Looper looper, @NonNull Context context, @NonNull String logTag,
|
||||||
|
@NonNull NetworkCapabilities nc, @NonNull LinkProperties lp, int score,
|
||||||
|
@NonNull NetworkAgentConfig config, int providerId, @NonNull NetworkInfo ni,
|
||||||
|
boolean legacy) {
|
||||||
mHandler = new NetworkAgentHandler(looper);
|
mHandler = new NetworkAgentHandler(looper);
|
||||||
LOG_TAG = logTag;
|
LOG_TAG = logTag;
|
||||||
mContext = context;
|
|
||||||
mIsLegacy = legacy;
|
mIsLegacy = legacy;
|
||||||
mNetworkInfo = new NetworkInfo(ni);
|
mNetworkInfo = new NetworkInfo(ni);
|
||||||
this.providerId = providerId;
|
this.providerId = providerId;
|
||||||
@@ -340,12 +363,8 @@ public abstract class NetworkAgent {
|
|||||||
throw new IllegalArgumentException();
|
throw new IllegalArgumentException();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (VDBG) log("Registering NetworkAgent");
|
mInitialConfiguration = new InitialConfiguration(context, new NetworkCapabilities(nc),
|
||||||
ConnectivityManager cm = (ConnectivityManager)mContext.getSystemService(
|
new LinkProperties(lp), score, config, ni);
|
||||||
Context.CONNECTIVITY_SERVICE);
|
|
||||||
network = cm.registerNetworkAgent(new Messenger(mHandler), new NetworkInfo(ni),
|
|
||||||
new LinkProperties(lp), new NetworkCapabilities(nc), score, config,
|
|
||||||
providerId);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private class NetworkAgentHandler extends Handler {
|
private class NetworkAgentHandler extends Handler {
|
||||||
@@ -467,6 +486,32 @@ public abstract class NetworkAgent {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register this network agent with ConnectivityService.
|
||||||
|
* @return the Network associated with this network agent (which can also be obtained later
|
||||||
|
* by calling getNetwork() on this agent).
|
||||||
|
*/
|
||||||
|
@NonNull
|
||||||
|
public Network register() {
|
||||||
|
if (VDBG) log("Registering NetworkAgent");
|
||||||
|
final ConnectivityManager cm = (ConnectivityManager) mInitialConfiguration.context
|
||||||
|
.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||||
|
mNetwork = cm.registerNetworkAgent(new Messenger(mHandler),
|
||||||
|
new NetworkInfo(mInitialConfiguration.info),
|
||||||
|
mInitialConfiguration.properties, mInitialConfiguration.capabilities,
|
||||||
|
mInitialConfiguration.score, mInitialConfiguration.config, providerId);
|
||||||
|
mInitialConfiguration = null; // All this memory can now be GC'd
|
||||||
|
return mNetwork;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return The Network associated with this agent, or null if it's not registered yet.
|
||||||
|
*/
|
||||||
|
@Nullable
|
||||||
|
public Network getNetwork() {
|
||||||
|
return mNetwork;
|
||||||
|
}
|
||||||
|
|
||||||
private void queueOrSendMessage(int what, Object obj) {
|
private void queueOrSendMessage(int what, Object obj) {
|
||||||
queueOrSendMessage(what, 0, 0, obj);
|
queueOrSendMessage(what, 0, 0, obj);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -219,7 +219,7 @@ class TestNetworkService extends ITestNetworkManager.Stub {
|
|||||||
// Has to be in TestNetworkAgent to ensure all teardown codepaths properly clean up
|
// Has to be in TestNetworkAgent to ensure all teardown codepaths properly clean up
|
||||||
// resources, even for binder death or unwanted calls.
|
// resources, even for binder death or unwanted calls.
|
||||||
synchronized (mTestNetworkTracker) {
|
synchronized (mTestNetworkTracker) {
|
||||||
mTestNetworkTracker.remove(network.netId);
|
mTestNetworkTracker.remove(getNetwork().netId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -338,7 +338,7 @@ class TestNetworkService extends ITestNetworkManager.Stub {
|
|||||||
callingUid,
|
callingUid,
|
||||||
binder);
|
binder);
|
||||||
|
|
||||||
mTestNetworkTracker.put(agent.network.netId, agent);
|
mTestNetworkTracker.put(agent.getNetwork().netId, agent);
|
||||||
}
|
}
|
||||||
} catch (SocketException e) {
|
} catch (SocketException e) {
|
||||||
throw new UncheckedIOException(e);
|
throw new UncheckedIOException(e);
|
||||||
|
|||||||
@@ -985,7 +985,11 @@ public class Vpn {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public int getNetId() {
|
public int getNetId() {
|
||||||
return mNetworkAgent != null ? mNetworkAgent.network.netId : NETID_UNSET;
|
final NetworkAgent agent = mNetworkAgent;
|
||||||
|
if (null == agent) return NETID_UNSET;
|
||||||
|
final Network network = agent.getNetwork();
|
||||||
|
if (null == network) return NETID_UNSET;
|
||||||
|
return network.netId;
|
||||||
}
|
}
|
||||||
|
|
||||||
private LinkProperties makeLinkProperties() {
|
private LinkProperties makeLinkProperties() {
|
||||||
|
|||||||
@@ -222,7 +222,7 @@ public class NetworkAgentWrapper implements TestableNetworkCallback.HasNetwork {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Network getNetwork() {
|
public Network getNetwork() {
|
||||||
return mNetworkAgent.network;
|
return mNetworkAgent.getNetwork();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void expectPreventReconnectReceived(long timeoutMs) {
|
public void expectPreventReconnectReceived(long timeoutMs) {
|
||||||
|
|||||||
@@ -647,7 +647,7 @@ public class ConnectivityServiceTest {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
assertEquals(na.network.netId, nmNetworkCaptor.getValue().netId);
|
assertEquals(na.getNetwork().netId, nmNetworkCaptor.getValue().netId);
|
||||||
mNmCallbacks = nmCbCaptor.getValue();
|
mNmCallbacks = nmCbCaptor.getValue();
|
||||||
|
|
||||||
mNmCallbacks.onNetworkMonitorCreated(mNetworkMonitor);
|
mNmCallbacks.onNetworkMonitorCreated(mNetworkMonitor);
|
||||||
|
|||||||
Reference in New Issue
Block a user