Merge "make UpsreamNetworkMonitor the sole source of upstream network information" am: 30a1cf96bb

am: cfaeabb3e3

Change-Id: Idfebb9b79074f21aeccf773cac98aa7839bda9da
This commit is contained in:
Erik Kline
2017-06-13 08:36:14 +00:00
committed by android-build-merger
3 changed files with 45 additions and 46 deletions

View File

@@ -1259,9 +1259,9 @@ public class Tethering extends BaseNetworkObserver {
protected void chooseUpstreamType(boolean tryCell) { protected void chooseUpstreamType(boolean tryCell) {
updateConfiguration(); // TODO - remove? updateConfiguration(); // TODO - remove?
final int upstreamType = mUpstreamNetworkMonitor.selectPreferredUpstreamType( final NetworkState ns = mUpstreamNetworkMonitor.selectPreferredUpstreamType(
mConfig.preferredUpstreamIfaceTypes); mConfig.preferredUpstreamIfaceTypes);
if (upstreamType == ConnectivityManager.TYPE_NONE) { if (ns == null) {
if (tryCell) { if (tryCell) {
mUpstreamNetworkMonitor.registerMobileNetworkRequest(); mUpstreamNetworkMonitor.registerMobileNetworkRequest();
// We think mobile should be coming up; don't set a retry. // We think mobile should be coming up; don't set a retry.
@@ -1269,40 +1269,30 @@ public class Tethering extends BaseNetworkObserver {
sendMessageDelayed(CMD_RETRY_UPSTREAM, UPSTREAM_SETTLE_TIME_MS); sendMessageDelayed(CMD_RETRY_UPSTREAM, UPSTREAM_SETTLE_TIME_MS);
} }
} }
setUpstreamByType(upstreamType); setUpstreamByType(ns);
} }
protected void setUpstreamByType(int upType) { protected void setUpstreamByType(NetworkState ns) {
final ConnectivityManager cm = getConnectivityManager();
Network network = null;
String iface = null; String iface = null;
if (upType != ConnectivityManager.TYPE_NONE) { if (ns != null && ns.linkProperties != null) {
LinkProperties linkProperties = cm.getLinkProperties(upType); // Find the interface with the default IPv4 route. It may be the
if (linkProperties != null) { // interface described by linkProperties, or one of the interfaces
// Find the interface with the default IPv4 route. It may be the // stacked on top of it.
// interface described by linkProperties, or one of the interfaces Log.i(TAG, "Finding IPv4 upstream interface on: " + ns.linkProperties);
// stacked on top of it. RouteInfo ipv4Default = RouteInfo.selectBestRoute(
Log.i(TAG, "Finding IPv4 upstream interface on: " + linkProperties); ns.linkProperties.getAllRoutes(), Inet4Address.ANY);
RouteInfo ipv4Default = RouteInfo.selectBestRoute( if (ipv4Default != null) {
linkProperties.getAllRoutes(), Inet4Address.ANY); iface = ipv4Default.getInterface();
if (ipv4Default != null) { Log.i(TAG, "Found interface " + ipv4Default.getInterface());
iface = ipv4Default.getInterface(); } else {
Log.i(TAG, "Found interface " + ipv4Default.getInterface()); Log.i(TAG, "No IPv4 upstream interface, giving up.");
} else {
Log.i(TAG, "No IPv4 upstream interface, giving up.");
}
}
if (iface != null) {
network = cm.getNetworkForType(upType);
if (network == null) {
Log.e(TAG, "No Network for upstream type " + upType + "!");
}
setDnsForwarders(network, linkProperties);
} }
} }
if (iface != null) {
setDnsForwarders(ns.network, ns.linkProperties);
}
notifyTetheredOfNewUpstreamIface(iface); notifyTetheredOfNewUpstreamIface(iface);
NetworkState ns = mUpstreamNetworkMonitor.lookup(network);
if (ns != null && pertainsToCurrentUpstream(ns)) { if (ns != null && pertainsToCurrentUpstream(ns)) {
// If we already have NetworkState for this network examine // If we already have NetworkState for this network examine
// it immediately, because there likely will be no second // it immediately, because there likely will be no second

View File

@@ -174,10 +174,6 @@ public class UpstreamNetworkMonitor {
mMobileNetworkCallback = null; mMobileNetworkCallback = null;
} }
public NetworkState lookup(Network network) {
return (network != null) ? mNetworkMap.get(network) : null;
}
// So many TODOs here, but chief among them is: make this functionality an // So many TODOs here, but chief among them is: make this functionality an
// integral part of this class such that whenever a higher priority network // integral part of this class such that whenever a higher priority network
// becomes available and useful we (a) file a request to keep it up as // becomes available and useful we (a) file a request to keep it up as
@@ -185,7 +181,7 @@ public class UpstreamNetworkMonitor {
// passing LinkProperties up to Tethering). // passing LinkProperties up to Tethering).
// //
// Next TODO: return NetworkState instead of just the type. // Next TODO: return NetworkState instead of just the type.
public int selectPreferredUpstreamType(Iterable<Integer> preferredTypes) { public NetworkState selectPreferredUpstreamType(Iterable<Integer> preferredTypes) {
final TypeStatePair typeStatePair = findFirstAvailableUpstreamByType( final TypeStatePair typeStatePair = findFirstAvailableUpstreamByType(
mNetworkMap.values(), preferredTypes); mNetworkMap.values(), preferredTypes);
@@ -210,7 +206,7 @@ public class UpstreamNetworkMonitor {
break; break;
} }
return typeStatePair.type; return typeStatePair.ns;
} }
private void handleAvailable(int callbackType, Network network) { private void handleAvailable(int callbackType, Network network) {

View File

@@ -47,6 +47,7 @@ import android.net.IConnectivityManager;
import android.net.Network; import android.net.Network;
import android.net.NetworkCapabilities; import android.net.NetworkCapabilities;
import android.net.NetworkRequest; import android.net.NetworkRequest;
import android.net.NetworkState;
import android.net.util.SharedLog; import android.net.util.SharedLog;
import android.support.test.filters.SmallTest; import android.support.test.filters.SmallTest;
@@ -253,31 +254,32 @@ public class UpstreamNetworkMonitorTest {
mUNM.start(); mUNM.start();
// There are no networks, so there is nothing to select. // There are no networks, so there is nothing to select.
assertEquals(TYPE_NONE, mUNM.selectPreferredUpstreamType(preferredTypes)); assertSatisfiesLegacyType(TYPE_NONE, mUNM.selectPreferredUpstreamType(preferredTypes));
final TestNetworkAgent wifiAgent = new TestNetworkAgent(mCM, TRANSPORT_WIFI); final TestNetworkAgent wifiAgent = new TestNetworkAgent(mCM, TRANSPORT_WIFI);
wifiAgent.fakeConnect(); wifiAgent.fakeConnect();
// WiFi is up, we should prefer it. // WiFi is up, we should prefer it.
assertEquals(TYPE_WIFI, mUNM.selectPreferredUpstreamType(preferredTypes)); assertSatisfiesLegacyType(TYPE_WIFI, mUNM.selectPreferredUpstreamType(preferredTypes));
wifiAgent.fakeDisconnect(); wifiAgent.fakeDisconnect();
// There are no networks, so there is nothing to select. // There are no networks, so there is nothing to select.
assertEquals(TYPE_NONE, mUNM.selectPreferredUpstreamType(preferredTypes)); assertSatisfiesLegacyType(TYPE_NONE, mUNM.selectPreferredUpstreamType(preferredTypes));
final TestNetworkAgent cellAgent = new TestNetworkAgent(mCM, TRANSPORT_CELLULAR); final TestNetworkAgent cellAgent = new TestNetworkAgent(mCM, TRANSPORT_CELLULAR);
cellAgent.fakeConnect(); cellAgent.fakeConnect();
assertEquals(TYPE_NONE, mUNM.selectPreferredUpstreamType(preferredTypes)); assertSatisfiesLegacyType(TYPE_NONE, mUNM.selectPreferredUpstreamType(preferredTypes));
preferredTypes.add(TYPE_MOBILE_DUN); preferredTypes.add(TYPE_MOBILE_DUN);
// This is coupled with preferred types in TetheringConfiguration. // This is coupled with preferred types in TetheringConfiguration.
mUNM.updateMobileRequiresDun(true); mUNM.updateMobileRequiresDun(true);
// DUN is available, but only use regular cell: no upstream selected. // DUN is available, but only use regular cell: no upstream selected.
assertEquals(TYPE_NONE, mUNM.selectPreferredUpstreamType(preferredTypes)); assertSatisfiesLegacyType(TYPE_NONE, mUNM.selectPreferredUpstreamType(preferredTypes));
preferredTypes.remove(TYPE_MOBILE_DUN); preferredTypes.remove(TYPE_MOBILE_DUN);
// No WiFi, but our preferred flavour of cell is up. // No WiFi, but our preferred flavour of cell is up.
preferredTypes.add(TYPE_MOBILE_HIPRI); preferredTypes.add(TYPE_MOBILE_HIPRI);
// This is coupled with preferred types in TetheringConfiguration. // This is coupled with preferred types in TetheringConfiguration.
mUNM.updateMobileRequiresDun(false); mUNM.updateMobileRequiresDun(false);
assertEquals(TYPE_MOBILE_HIPRI, mUNM.selectPreferredUpstreamType(preferredTypes)); assertSatisfiesLegacyType(TYPE_MOBILE_HIPRI,
mUNM.selectPreferredUpstreamType(preferredTypes));
// Check to see we filed an explicit request. // Check to see we filed an explicit request.
assertEquals(1, mCM.requested.size()); assertEquals(1, mCM.requested.size());
NetworkRequest netReq = (NetworkRequest) mCM.requested.values().toArray()[0]; NetworkRequest netReq = (NetworkRequest) mCM.requested.values().toArray()[0];
@@ -286,25 +288,26 @@ public class UpstreamNetworkMonitorTest {
wifiAgent.fakeConnect(); wifiAgent.fakeConnect();
// WiFi is up, and we should prefer it over cell. // WiFi is up, and we should prefer it over cell.
assertEquals(TYPE_WIFI, mUNM.selectPreferredUpstreamType(preferredTypes)); assertSatisfiesLegacyType(TYPE_WIFI, mUNM.selectPreferredUpstreamType(preferredTypes));
assertEquals(0, mCM.requested.size()); assertEquals(0, mCM.requested.size());
preferredTypes.remove(TYPE_MOBILE_HIPRI); preferredTypes.remove(TYPE_MOBILE_HIPRI);
preferredTypes.add(TYPE_MOBILE_DUN); preferredTypes.add(TYPE_MOBILE_DUN);
// This is coupled with preferred types in TetheringConfiguration. // This is coupled with preferred types in TetheringConfiguration.
mUNM.updateMobileRequiresDun(true); mUNM.updateMobileRequiresDun(true);
assertEquals(TYPE_WIFI, mUNM.selectPreferredUpstreamType(preferredTypes)); assertSatisfiesLegacyType(TYPE_WIFI, mUNM.selectPreferredUpstreamType(preferredTypes));
final TestNetworkAgent dunAgent = new TestNetworkAgent(mCM, TRANSPORT_CELLULAR); final TestNetworkAgent dunAgent = new TestNetworkAgent(mCM, TRANSPORT_CELLULAR);
dunAgent.networkCapabilities.addCapability(NET_CAPABILITY_DUN); dunAgent.networkCapabilities.addCapability(NET_CAPABILITY_DUN);
dunAgent.fakeConnect(); dunAgent.fakeConnect();
// WiFi is still preferred. // WiFi is still preferred.
assertEquals(TYPE_WIFI, mUNM.selectPreferredUpstreamType(preferredTypes)); assertSatisfiesLegacyType(TYPE_WIFI, mUNM.selectPreferredUpstreamType(preferredTypes));
// WiFi goes down, cell and DUN are still up but only DUN is preferred. // WiFi goes down, cell and DUN are still up but only DUN is preferred.
wifiAgent.fakeDisconnect(); wifiAgent.fakeDisconnect();
assertEquals(TYPE_MOBILE_DUN, mUNM.selectPreferredUpstreamType(preferredTypes)); assertSatisfiesLegacyType(TYPE_MOBILE_DUN,
mUNM.selectPreferredUpstreamType(preferredTypes));
// Check to see we filed an explicit request. // Check to see we filed an explicit request.
assertEquals(1, mCM.requested.size()); assertEquals(1, mCM.requested.size());
netReq = (NetworkRequest) mCM.requested.values().toArray()[0]; netReq = (NetworkRequest) mCM.requested.values().toArray()[0];
@@ -312,6 +315,16 @@ public class UpstreamNetworkMonitorTest {
assertTrue(netReq.networkCapabilities.hasCapability(NET_CAPABILITY_DUN)); assertTrue(netReq.networkCapabilities.hasCapability(NET_CAPABILITY_DUN));
} }
private void assertSatisfiesLegacyType(int legacyType, NetworkState ns) {
if (legacyType == TYPE_NONE) {
assertTrue(ns == null);
return;
}
final NetworkCapabilities nc = ConnectivityManager.networkCapabilitiesForType(legacyType);
assertTrue(nc.satisfiedByNetworkCapabilities(ns.networkCapabilities));
}
private void assertUpstreamTypeRequested(int upstreamType) throws Exception { private void assertUpstreamTypeRequested(int upstreamType) throws Exception {
assertEquals(1, mCM.requested.size()); assertEquals(1, mCM.requested.size());
assertEquals(1, mCM.legacyTypeMap.size()); assertEquals(1, mCM.legacyTypeMap.size());