Merge "Allow wifi and ethenet connected at same time."

This commit is contained in:
Treehugger Robot
2018-10-02 17:09:16 +00:00
committed by Gerrit Code Review
2 changed files with 45 additions and 22 deletions

View File

@@ -393,9 +393,9 @@ public class ConnectivityService extends IConnectivityManager.Stub
private static final int EVENT_PROMPT_UNVALIDATED = 29; private static final int EVENT_PROMPT_UNVALIDATED = 29;
/** /**
* used internally to (re)configure mobile data always-on settings. * used internally to (re)configure always-on networks.
*/ */
private static final int EVENT_CONFIGURE_MOBILE_DATA_ALWAYS_ON = 30; private static final int EVENT_CONFIGURE_ALWAYS_ON_NETWORKS = 30;
/** /**
* used to add a network listener with a pending intent * used to add a network listener with a pending intent
@@ -751,6 +751,12 @@ public class ConnectivityService extends IConnectivityManager.Stub
mDefaultMobileDataRequest = createDefaultInternetRequestForTransport( mDefaultMobileDataRequest = createDefaultInternetRequestForTransport(
NetworkCapabilities.TRANSPORT_CELLULAR, NetworkRequest.Type.BACKGROUND_REQUEST); NetworkCapabilities.TRANSPORT_CELLULAR, NetworkRequest.Type.BACKGROUND_REQUEST);
// The default WiFi request is a background request so that apps using WiFi are
// migrated to a better network (typically ethernet) when one comes up, instead
// of staying on WiFi forever.
mDefaultWifiRequest = createDefaultInternetRequestForTransport(
NetworkCapabilities.TRANSPORT_WIFI, NetworkRequest.Type.BACKGROUND_REQUEST);
mHandlerThread = new HandlerThread("ConnectivityServiceThread"); mHandlerThread = new HandlerThread("ConnectivityServiceThread");
mHandlerThread.start(); mHandlerThread.start();
mHandler = new InternalHandler(mHandlerThread.getLooper()); mHandler = new InternalHandler(mHandlerThread.getLooper());
@@ -948,8 +954,8 @@ public class ConnectivityService extends IConnectivityManager.Stub
// 2. Give FakeSettingsProvider an alternative notification mechanism and have the test use it // 2. Give FakeSettingsProvider an alternative notification mechanism and have the test use it
// by subclassing SettingsObserver. // by subclassing SettingsObserver.
@VisibleForTesting @VisibleForTesting
void updateMobileDataAlwaysOn() { void updateAlwaysOnNetworks() {
mHandler.sendEmptyMessage(EVENT_CONFIGURE_MOBILE_DATA_ALWAYS_ON); mHandler.sendEmptyMessage(EVENT_CONFIGURE_ALWAYS_ON_NETWORKS);
} }
// See FakeSettingsProvider comment above. // See FakeSettingsProvider comment above.
@@ -958,22 +964,30 @@ public class ConnectivityService extends IConnectivityManager.Stub
mHandler.sendEmptyMessage(EVENT_PRIVATE_DNS_SETTINGS_CHANGED); mHandler.sendEmptyMessage(EVENT_PRIVATE_DNS_SETTINGS_CHANGED);
} }
private void handleMobileDataAlwaysOn() { private void handleAlwaysOnNetworkRequest(
NetworkRequest networkRequest, String settingName, boolean defaultValue) {
final boolean enable = toBool(Settings.Global.getInt( final boolean enable = toBool(Settings.Global.getInt(
mContext.getContentResolver(), Settings.Global.MOBILE_DATA_ALWAYS_ON, 1)); mContext.getContentResolver(), settingName, encodeBool(defaultValue)));
final boolean isEnabled = (mNetworkRequests.get(mDefaultMobileDataRequest) != null); final boolean isEnabled = (mNetworkRequests.get(networkRequest) != null);
if (enable == isEnabled) { if (enable == isEnabled) {
return; // Nothing to do. return; // Nothing to do.
} }
if (enable) { if (enable) {
handleRegisterNetworkRequest(new NetworkRequestInfo( handleRegisterNetworkRequest(new NetworkRequestInfo(
null, mDefaultMobileDataRequest, new Binder())); null, networkRequest, new Binder()));
} else { } else {
handleReleaseNetworkRequest(mDefaultMobileDataRequest, Process.SYSTEM_UID); handleReleaseNetworkRequest(networkRequest, Process.SYSTEM_UID);
} }
} }
private void handleConfigureAlwaysOnNetworks() {
handleAlwaysOnNetworkRequest(
mDefaultMobileDataRequest,Settings.Global.MOBILE_DATA_ALWAYS_ON, true);
handleAlwaysOnNetworkRequest(mDefaultWifiRequest, Settings.Global.WIFI_ALWAYS_REQUESTED,
false);
}
private void registerSettingsCallbacks() { private void registerSettingsCallbacks() {
// Watch for global HTTP proxy changes. // Watch for global HTTP proxy changes.
mSettingsObserver.observe( mSettingsObserver.observe(
@@ -983,7 +997,12 @@ public class ConnectivityService extends IConnectivityManager.Stub
// Watch for whether or not to keep mobile data always on. // Watch for whether or not to keep mobile data always on.
mSettingsObserver.observe( mSettingsObserver.observe(
Settings.Global.getUriFor(Settings.Global.MOBILE_DATA_ALWAYS_ON), Settings.Global.getUriFor(Settings.Global.MOBILE_DATA_ALWAYS_ON),
EVENT_CONFIGURE_MOBILE_DATA_ALWAYS_ON); EVENT_CONFIGURE_ALWAYS_ON_NETWORKS);
// Watch for whether or not to keep wifi always on.
mSettingsObserver.observe(
Settings.Global.getUriFor(Settings.Global.WIFI_ALWAYS_REQUESTED),
EVENT_CONFIGURE_ALWAYS_ON_NETWORKS);
} }
private void registerPrivateDnsSettingsCallbacks() { private void registerPrivateDnsSettingsCallbacks() {
@@ -1835,8 +1854,8 @@ public class ConnectivityService extends IConnectivityManager.Stub
// for user to unlock device too. // for user to unlock device too.
updateLockdownVpn(); updateLockdownVpn();
// Configure whether mobile data is always on. // Create network requests for always-on networks.
mHandler.sendMessage(mHandler.obtainMessage(EVENT_CONFIGURE_MOBILE_DATA_ALWAYS_ON)); mHandler.sendMessage(mHandler.obtainMessage(EVENT_CONFIGURE_ALWAYS_ON_NETWORKS));
mHandler.sendMessage(mHandler.obtainMessage(EVENT_SYSTEM_READY)); mHandler.sendMessage(mHandler.obtainMessage(EVENT_SYSTEM_READY));
@@ -3125,8 +3144,8 @@ public class ConnectivityService extends IConnectivityManager.Stub
handlePromptUnvalidated((Network) msg.obj); handlePromptUnvalidated((Network) msg.obj);
break; break;
} }
case EVENT_CONFIGURE_MOBILE_DATA_ALWAYS_ON: { case EVENT_CONFIGURE_ALWAYS_ON_NETWORKS: {
handleMobileDataAlwaysOn(); handleConfigureAlwaysOnNetworks();
break; break;
} }
// Sent by KeepaliveTracker to process an app request on the state machine thread. // Sent by KeepaliveTracker to process an app request on the state machine thread.
@@ -4554,6 +4573,10 @@ public class ConnectivityService extends IConnectivityManager.Stub
// priority networks like Wi-Fi are active. // priority networks like Wi-Fi are active.
private final NetworkRequest mDefaultMobileDataRequest; private final NetworkRequest mDefaultMobileDataRequest;
// Request used to optionally keep wifi data active even when higher
// priority networks like ethernet are active.
private final NetworkRequest mDefaultWifiRequest;
private NetworkAgentInfo getNetworkForRequest(int requestId) { private NetworkAgentInfo getNetworkForRequest(int requestId) {
synchronized (mNetworkForRequestId) { synchronized (mNetworkForRequestId) {
return mNetworkForRequestId.get(requestId); return mNetworkForRequestId.get(requestId);

View File

@@ -1070,13 +1070,13 @@ public class ConnectivityServiceTest {
// Ensure that the default setting for Captive Portals is used for most tests // Ensure that the default setting for Captive Portals is used for most tests
setCaptivePortalMode(Settings.Global.CAPTIVE_PORTAL_MODE_PROMPT); setCaptivePortalMode(Settings.Global.CAPTIVE_PORTAL_MODE_PROMPT);
setMobileDataAlwaysOn(false); setAlwaysOnNetworks(false);
setPrivateDnsSettings(PRIVATE_DNS_MODE_OFF, "ignored.example.com"); setPrivateDnsSettings(PRIVATE_DNS_MODE_OFF, "ignored.example.com");
} }
@After @After
public void tearDown() throws Exception { public void tearDown() throws Exception {
setMobileDataAlwaysOn(false); setAlwaysOnNetworks(false);
if (mCellNetworkAgent != null) { if (mCellNetworkAgent != null) {
mCellNetworkAgent.disconnect(); mCellNetworkAgent.disconnect();
mCellNetworkAgent = null; mCellNetworkAgent = null;
@@ -2027,7 +2027,7 @@ public class ConnectivityServiceTest {
@Test @Test
public void testNetworkGoesIntoBackgroundAfterLinger() { public void testNetworkGoesIntoBackgroundAfterLinger() {
setMobileDataAlwaysOn(true); setAlwaysOnNetworks(true);
NetworkRequest request = new NetworkRequest.Builder() NetworkRequest request = new NetworkRequest.Builder()
.clearCapabilities() .clearCapabilities()
.build(); .build();
@@ -2772,10 +2772,10 @@ public class ConnectivityServiceTest {
Settings.Global.putInt(cr, Settings.Global.CAPTIVE_PORTAL_MODE, mode); Settings.Global.putInt(cr, Settings.Global.CAPTIVE_PORTAL_MODE, mode);
} }
private void setMobileDataAlwaysOn(boolean enable) { private void setAlwaysOnNetworks(boolean enable) {
ContentResolver cr = mServiceContext.getContentResolver(); ContentResolver cr = mServiceContext.getContentResolver();
Settings.Global.putInt(cr, Settings.Global.MOBILE_DATA_ALWAYS_ON, enable ? 1 : 0); Settings.Global.putInt(cr, Settings.Global.MOBILE_DATA_ALWAYS_ON, enable ? 1 : 0);
mService.updateMobileDataAlwaysOn(); mService.updateAlwaysOnNetworks();
waitForIdle(); waitForIdle();
} }
@@ -2797,7 +2797,7 @@ public class ConnectivityServiceTest {
public void testBackgroundNetworks() throws Exception { public void testBackgroundNetworks() throws Exception {
// Create a background request. We can't do this ourselves because ConnectivityService // Create a background request. We can't do this ourselves because ConnectivityService
// doesn't have an API for it. So just turn on mobile data always on. // doesn't have an API for it. So just turn on mobile data always on.
setMobileDataAlwaysOn(true); setAlwaysOnNetworks(true);
final NetworkRequest request = new NetworkRequest.Builder().build(); final NetworkRequest request = new NetworkRequest.Builder().build();
final NetworkRequest fgRequest = new NetworkRequest.Builder() final NetworkRequest fgRequest = new NetworkRequest.Builder()
.addCapability(NET_CAPABILITY_FOREGROUND).build(); .addCapability(NET_CAPABILITY_FOREGROUND).build();
@@ -2995,7 +2995,7 @@ public class ConnectivityServiceTest {
// Turn on mobile data always on. The factory starts looking again. // Turn on mobile data always on. The factory starts looking again.
testFactory.expectAddRequests(1); testFactory.expectAddRequests(1);
setMobileDataAlwaysOn(true); setAlwaysOnNetworks(true);
testFactory.waitForNetworkRequests(2); testFactory.waitForNetworkRequests(2);
assertTrue(testFactory.getMyStartRequested()); assertTrue(testFactory.getMyStartRequested());
@@ -3015,7 +3015,7 @@ public class ConnectivityServiceTest {
// Turn off mobile data always on and expect the request to disappear... // Turn off mobile data always on and expect the request to disappear...
testFactory.expectRemoveRequests(1); testFactory.expectRemoveRequests(1);
setMobileDataAlwaysOn(false); setAlwaysOnNetworks(false);
testFactory.waitForNetworkRequests(1); testFactory.waitForNetworkRequests(1);
// ... and cell data to be torn down. // ... and cell data to be torn down.