Merge "Add Network inspection API."
This commit is contained in:
committed by
Android (Google) Code Review
commit
11e39d2fb7
@@ -743,7 +743,7 @@ public class ConnectivityManager {
|
|||||||
* network type or {@code null} if the type is not
|
* network type or {@code null} if the type is not
|
||||||
* supported by the device.
|
* supported by the device.
|
||||||
*
|
*
|
||||||
* <p>This method requires the call to hold the permission
|
* <p>This method requires the caller to hold the permission
|
||||||
* {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
|
* {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
|
||||||
*/
|
*/
|
||||||
public NetworkInfo getNetworkInfo(int networkType) {
|
public NetworkInfo getNetworkInfo(int networkType) {
|
||||||
@@ -754,6 +754,27 @@ public class ConnectivityManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns connection status information about a particular
|
||||||
|
* Network.
|
||||||
|
*
|
||||||
|
* @param network {@link Network} specifying which network
|
||||||
|
* in which you're interested.
|
||||||
|
* @return a {@link NetworkInfo} object for the requested
|
||||||
|
* network or {@code null} if the {@code Network}
|
||||||
|
* is not valid.
|
||||||
|
*
|
||||||
|
* <p>This method requires the caller to hold the permission
|
||||||
|
* {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
|
||||||
|
*/
|
||||||
|
public NetworkInfo getNetworkInfo(Network network) {
|
||||||
|
try {
|
||||||
|
return mService.getNetworkInfoForNetwork(network);
|
||||||
|
} catch (RemoteException e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns connection status information about all network
|
* Returns connection status information about all network
|
||||||
* types supported by the device.
|
* types supported by the device.
|
||||||
@@ -761,7 +782,7 @@ public class ConnectivityManager {
|
|||||||
* @return an array of {@link NetworkInfo} objects. Check each
|
* @return an array of {@link NetworkInfo} objects. Check each
|
||||||
* {@link NetworkInfo#getType} for which type each applies.
|
* {@link NetworkInfo#getType} for which type each applies.
|
||||||
*
|
*
|
||||||
* <p>This method requires the call to hold the permission
|
* <p>This method requires the caller to hold the permission
|
||||||
* {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
|
* {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
|
||||||
*/
|
*/
|
||||||
public NetworkInfo[] getAllNetworkInfo() {
|
public NetworkInfo[] getAllNetworkInfo() {
|
||||||
@@ -772,6 +793,23 @@ public class ConnectivityManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns an array of all {@link Network} currently tracked by the
|
||||||
|
* framework.
|
||||||
|
*
|
||||||
|
* @return an array of {@link Network} objects.
|
||||||
|
*
|
||||||
|
* <p>This method requires the caller to hold the permission
|
||||||
|
* {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
|
||||||
|
*/
|
||||||
|
public Network[] getAllNetworks() {
|
||||||
|
try {
|
||||||
|
return mService.getAllNetworks();
|
||||||
|
} catch (RemoteException e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns details about the Provisioning or currently active default data network. When
|
* Returns details about the Provisioning or currently active default data network. When
|
||||||
* connected, this network is the default route for outgoing connections.
|
* connected, this network is the default route for outgoing connections.
|
||||||
|
|||||||
@@ -48,7 +48,9 @@ interface IConnectivityManager
|
|||||||
NetworkInfo getActiveNetworkInfo();
|
NetworkInfo getActiveNetworkInfo();
|
||||||
NetworkInfo getActiveNetworkInfoForUid(int uid);
|
NetworkInfo getActiveNetworkInfoForUid(int uid);
|
||||||
NetworkInfo getNetworkInfo(int networkType);
|
NetworkInfo getNetworkInfo(int networkType);
|
||||||
|
NetworkInfo getNetworkInfoForNetwork(in Network network);
|
||||||
NetworkInfo[] getAllNetworkInfo();
|
NetworkInfo[] getAllNetworkInfo();
|
||||||
|
Network[] getAllNetworks();
|
||||||
|
|
||||||
NetworkInfo getProvisioningOrActiveNetworkInfo();
|
NetworkInfo getProvisioningOrActiveNetworkInfo();
|
||||||
|
|
||||||
|
|||||||
@@ -1071,6 +1071,10 @@ public class ConnectivityService extends IConnectivityManager.Stub {
|
|||||||
*/
|
*/
|
||||||
private NetworkInfo getFilteredNetworkInfo(int networkType, int uid) {
|
private NetworkInfo getFilteredNetworkInfo(int networkType, int uid) {
|
||||||
NetworkInfo info = getNetworkInfoForType(networkType);
|
NetworkInfo info = getNetworkInfoForType(networkType);
|
||||||
|
return getFilteredNetworkInfo(info, networkType, uid);
|
||||||
|
}
|
||||||
|
|
||||||
|
private NetworkInfo getFilteredNetworkInfo(NetworkInfo info, int networkType, int uid) {
|
||||||
if (isNetworkBlocked(networkType, uid)) {
|
if (isNetworkBlocked(networkType, uid)) {
|
||||||
// network is blocked; clone and override state
|
// network is blocked; clone and override state
|
||||||
info = new NetworkInfo(info);
|
info = new NetworkInfo(info);
|
||||||
@@ -1175,6 +1179,24 @@ public class ConnectivityService extends IConnectivityManager.Stub {
|
|||||||
return info;
|
return info;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public NetworkInfo getNetworkInfoForNetwork(Network network) {
|
||||||
|
enforceAccessPermission();
|
||||||
|
if (network == null) return null;
|
||||||
|
|
||||||
|
final int uid = Binder.getCallingUid();
|
||||||
|
NetworkAgentInfo nai = null;
|
||||||
|
synchronized (mNetworkForNetId) {
|
||||||
|
nai = mNetworkForNetId.get(network.netId);
|
||||||
|
}
|
||||||
|
if (nai == null) return null;
|
||||||
|
synchronized (nai) {
|
||||||
|
if (nai.networkInfo == null) return null;
|
||||||
|
|
||||||
|
return getFilteredNetworkInfo(nai.networkInfo, nai.networkInfo.getType(), uid);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NetworkInfo[] getAllNetworkInfo() {
|
public NetworkInfo[] getAllNetworkInfo() {
|
||||||
enforceAccessPermission();
|
enforceAccessPermission();
|
||||||
@@ -1191,6 +1213,18 @@ public class ConnectivityService extends IConnectivityManager.Stub {
|
|||||||
return result.toArray(new NetworkInfo[result.size()]);
|
return result.toArray(new NetworkInfo[result.size()]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Network[] getAllNetworks() {
|
||||||
|
enforceAccessPermission();
|
||||||
|
final ArrayList<Network> result = new ArrayList();
|
||||||
|
synchronized (mNetworkForNetId) {
|
||||||
|
for (int i = 0; i < mNetworkForNetId.size(); i++) {
|
||||||
|
result.add(new Network(mNetworkForNetId.valueAt(i).network));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result.toArray(new Network[result.size()]);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isNetworkSupported(int networkType) {
|
public boolean isNetworkSupported(int networkType) {
|
||||||
enforceAccessPermission();
|
enforceAccessPermission();
|
||||||
@@ -1223,16 +1257,31 @@ public class ConnectivityService extends IConnectivityManager.Stub {
|
|||||||
@Override
|
@Override
|
||||||
public LinkProperties getLinkProperties(Network network) {
|
public LinkProperties getLinkProperties(Network network) {
|
||||||
enforceAccessPermission();
|
enforceAccessPermission();
|
||||||
NetworkAgentInfo nai = mNetworkForNetId.get(network.netId);
|
NetworkAgentInfo nai = null;
|
||||||
if (nai != null) return new LinkProperties(nai.linkProperties);
|
synchronized (mNetworkForNetId) {
|
||||||
|
nai = mNetworkForNetId.get(network.netId);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (nai != null) {
|
||||||
|
synchronized (nai) {
|
||||||
|
return new LinkProperties(nai.linkProperties);
|
||||||
|
}
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NetworkCapabilities getNetworkCapabilities(Network network) {
|
public NetworkCapabilities getNetworkCapabilities(Network network) {
|
||||||
enforceAccessPermission();
|
enforceAccessPermission();
|
||||||
NetworkAgentInfo nai = mNetworkForNetId.get(network.netId);
|
NetworkAgentInfo nai = null;
|
||||||
if (nai != null) return new NetworkCapabilities(nai.networkCapabilities);
|
synchronized (mNetworkForNetId) {
|
||||||
|
nai = mNetworkForNetId.get(network.netId);
|
||||||
|
}
|
||||||
|
if (nai != null) {
|
||||||
|
synchronized (nai) {
|
||||||
|
return new NetworkCapabilities(nai.networkCapabilities);
|
||||||
|
}
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1777,8 +1826,10 @@ public class ConnectivityService extends IConnectivityManager.Stub {
|
|||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
DetailedState netState;
|
||||||
DetailedState netState = nai.networkInfo.getDetailedState();
|
synchronized (nai) {
|
||||||
|
netState = nai.networkInfo.getDetailedState();
|
||||||
|
}
|
||||||
|
|
||||||
if ((netState != DetailedState.CONNECTED &&
|
if ((netState != DetailedState.CONNECTED &&
|
||||||
netState != DetailedState.CAPTIVE_PORTAL_CHECK)) {
|
netState != DetailedState.CAPTIVE_PORTAL_CHECK)) {
|
||||||
@@ -1792,9 +1843,13 @@ public class ConnectivityService extends IConnectivityManager.Stub {
|
|||||||
final int uid = Binder.getCallingUid();
|
final int uid = Binder.getCallingUid();
|
||||||
final long token = Binder.clearCallingIdentity();
|
final long token = Binder.clearCallingIdentity();
|
||||||
try {
|
try {
|
||||||
LinkProperties lp = nai.linkProperties;
|
LinkProperties lp = null;
|
||||||
boolean ok = modifyRouteToAddress(lp, addr, ADD, TO_DEFAULT_TABLE, exempt,
|
int netId = INVALID_NET_ID;
|
||||||
nai.network.netId, uid);
|
synchronized (nai) {
|
||||||
|
lp = nai.linkProperties;
|
||||||
|
netId = nai.network.netId;
|
||||||
|
}
|
||||||
|
boolean ok = modifyRouteToAddress(lp, addr, ADD, TO_DEFAULT_TABLE, exempt, netId, uid);
|
||||||
if (DBG) log("requestRouteToHostAddress ok=" + ok);
|
if (DBG) log("requestRouteToHostAddress ok=" + ok);
|
||||||
return ok;
|
return ok;
|
||||||
} finally {
|
} finally {
|
||||||
@@ -3096,7 +3151,9 @@ public class ConnectivityService extends IConnectivityManager.Stub {
|
|||||||
} else {
|
} else {
|
||||||
if (VDBG) log("Update of Linkproperties for " + nai.name());
|
if (VDBG) log("Update of Linkproperties for " + nai.name());
|
||||||
LinkProperties oldLp = nai.linkProperties;
|
LinkProperties oldLp = nai.linkProperties;
|
||||||
|
synchronized (nai) {
|
||||||
nai.linkProperties = (LinkProperties)msg.obj;
|
nai.linkProperties = (LinkProperties)msg.obj;
|
||||||
|
}
|
||||||
updateLinkProperties(nai, oldLp);
|
updateLinkProperties(nai, oldLp);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@@ -3242,7 +3299,9 @@ public class ConnectivityService extends IConnectivityManager.Stub {
|
|||||||
loge("Error connecting NetworkAgent");
|
loge("Error connecting NetworkAgent");
|
||||||
NetworkAgentInfo nai = mNetworkAgentInfos.remove(msg.replyTo);
|
NetworkAgentInfo nai = mNetworkAgentInfos.remove(msg.replyTo);
|
||||||
if (nai != null) {
|
if (nai != null) {
|
||||||
|
synchronized (mNetworkForNetId) {
|
||||||
mNetworkForNetId.remove(nai.network.netId);
|
mNetworkForNetId.remove(nai.network.netId);
|
||||||
|
}
|
||||||
mLegacyTypeTracker.remove(nai);
|
mLegacyTypeTracker.remove(nai);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3275,7 +3334,9 @@ public class ConnectivityService extends IConnectivityManager.Stub {
|
|||||||
mNetworkAgentInfos.remove(msg.replyTo);
|
mNetworkAgentInfos.remove(msg.replyTo);
|
||||||
updateClat(null, nai.linkProperties, nai);
|
updateClat(null, nai.linkProperties, nai);
|
||||||
mLegacyTypeTracker.remove(nai);
|
mLegacyTypeTracker.remove(nai);
|
||||||
|
synchronized (mNetworkForNetId) {
|
||||||
mNetworkForNetId.remove(nai.network.netId);
|
mNetworkForNetId.remove(nai.network.netId);
|
||||||
|
}
|
||||||
// Since we've lost the network, go through all the requests that
|
// Since we've lost the network, go through all the requests that
|
||||||
// it was satisfying and see if any other factory can satisfy them.
|
// it was satisfying and see if any other factory can satisfy them.
|
||||||
final ArrayList<NetworkAgentInfo> toActivate = new ArrayList<NetworkAgentInfo>();
|
final ArrayList<NetworkAgentInfo> toActivate = new ArrayList<NetworkAgentInfo>();
|
||||||
@@ -5565,7 +5626,9 @@ public class ConnectivityService extends IConnectivityManager.Stub {
|
|||||||
private void handleRegisterNetworkAgent(NetworkAgentInfo na) {
|
private void handleRegisterNetworkAgent(NetworkAgentInfo na) {
|
||||||
if (VDBG) log("Got NetworkAgent Messenger");
|
if (VDBG) log("Got NetworkAgent Messenger");
|
||||||
mNetworkAgentInfos.put(na.messenger, na);
|
mNetworkAgentInfos.put(na.messenger, na);
|
||||||
|
synchronized (mNetworkForNetId) {
|
||||||
mNetworkForNetId.put(na.network.netId, na);
|
mNetworkForNetId.put(na.network.netId, na);
|
||||||
|
}
|
||||||
na.asyncChannel.connect(mContext, mTrackerHandler, na.messenger);
|
na.asyncChannel.connect(mContext, mTrackerHandler, na.messenger);
|
||||||
NetworkInfo networkInfo = na.networkInfo;
|
NetworkInfo networkInfo = na.networkInfo;
|
||||||
na.networkInfo = null;
|
na.networkInfo = null;
|
||||||
@@ -5710,8 +5773,10 @@ public class ConnectivityService extends IConnectivityManager.Stub {
|
|||||||
NetworkCapabilities networkCapabilities) {
|
NetworkCapabilities networkCapabilities) {
|
||||||
// TODO - what else here? Verify still satisfies everybody?
|
// TODO - what else here? Verify still satisfies everybody?
|
||||||
// Check if satisfies somebody new? call callbacks?
|
// Check if satisfies somebody new? call callbacks?
|
||||||
|
synchronized (networkAgent) {
|
||||||
networkAgent.networkCapabilities = networkCapabilities;
|
networkAgent.networkCapabilities = networkCapabilities;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void sendUpdatedScoreToFactories(NetworkRequest networkRequest, int score) {
|
private void sendUpdatedScoreToFactories(NetworkRequest networkRequest, int score) {
|
||||||
if (VDBG) log("sending new Min Network Score(" + score + "): " + networkRequest.toString());
|
if (VDBG) log("sending new Min Network Score(" + score + "): " + networkRequest.toString());
|
||||||
@@ -5924,8 +5989,11 @@ public class ConnectivityService extends IConnectivityManager.Stub {
|
|||||||
|
|
||||||
private void updateNetworkInfo(NetworkAgentInfo networkAgent, NetworkInfo newInfo) {
|
private void updateNetworkInfo(NetworkAgentInfo networkAgent, NetworkInfo newInfo) {
|
||||||
NetworkInfo.State state = newInfo.getState();
|
NetworkInfo.State state = newInfo.getState();
|
||||||
NetworkInfo oldInfo = networkAgent.networkInfo;
|
NetworkInfo oldInfo = null;
|
||||||
|
synchronized (networkAgent) {
|
||||||
|
oldInfo = networkAgent.networkInfo;
|
||||||
networkAgent.networkInfo = newInfo;
|
networkAgent.networkInfo = newInfo;
|
||||||
|
}
|
||||||
|
|
||||||
if (oldInfo != null && oldInfo.getState() == state) {
|
if (oldInfo != null && oldInfo.getState() == state) {
|
||||||
if (VDBG) log("ignoring duplicate network state non-change");
|
if (VDBG) log("ignoring duplicate network state non-change");
|
||||||
@@ -6054,9 +6122,12 @@ public class ConnectivityService extends IConnectivityManager.Stub {
|
|||||||
|
|
||||||
private LinkProperties getLinkPropertiesForTypeInternal(int networkType) {
|
private LinkProperties getLinkPropertiesForTypeInternal(int networkType) {
|
||||||
NetworkAgentInfo nai = mLegacyTypeTracker.getNetworkForType(networkType);
|
NetworkAgentInfo nai = mLegacyTypeTracker.getNetworkForType(networkType);
|
||||||
return (nai != null) ?
|
if (nai != null) {
|
||||||
new LinkProperties(nai.linkProperties) :
|
synchronized (nai) {
|
||||||
new LinkProperties();
|
return new LinkProperties(nai.linkProperties);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new LinkProperties();
|
||||||
}
|
}
|
||||||
|
|
||||||
private NetworkInfo getNetworkInfoForType(int networkType) {
|
private NetworkInfo getNetworkInfoForType(int networkType) {
|
||||||
@@ -6075,8 +6146,11 @@ public class ConnectivityService extends IConnectivityManager.Stub {
|
|||||||
|
|
||||||
private NetworkCapabilities getNetworkCapabilitiesForType(int networkType) {
|
private NetworkCapabilities getNetworkCapabilitiesForType(int networkType) {
|
||||||
NetworkAgentInfo nai = mLegacyTypeTracker.getNetworkForType(networkType);
|
NetworkAgentInfo nai = mLegacyTypeTracker.getNetworkForType(networkType);
|
||||||
return (nai != null) ?
|
if (nai != null) {
|
||||||
new NetworkCapabilities(nai.networkCapabilities) :
|
synchronized (nai) {
|
||||||
new NetworkCapabilities();
|
return new NetworkCapabilities(nai.networkCapabilities);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new NetworkCapabilities();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user