diff --git a/service/src/com/android/server/ConnectivityService.java b/service/src/com/android/server/ConnectivityService.java index 3fb0150344..53127d11e4 100755 --- a/service/src/com/android/server/ConnectivityService.java +++ b/service/src/com/android/server/ConnectivityService.java @@ -1703,7 +1703,8 @@ public class ConnectivityService extends IConnectivityManager.Stub mUserAllContext.registerReceiver(mPackageIntentReceiver, packageIntentFilter, null /* broadcastPermission */, mHandler); - mNetworkActivityTracker = new LegacyNetworkActivityTracker(mContext, mNetd, mHandler); + mNetworkActivityTracker = + new LegacyNetworkActivityTracker(mContext, mNetd, mHandler, mDeps.isAtLeastU()); final NetdCallback netdCallback = new NetdCallback(); try { @@ -11125,10 +11126,9 @@ public class ConnectivityService extends IConnectivityManager.Stub new RemoteCallbackList<>(); // Indicate the current system default network activity is active or not. // This needs to be volatile to allow non handler threads to read this value without lock. - // TODO: Remove initial value. Initial value is set to keep the existing behavior. - // This will be removed in following CL. - private volatile boolean mIsDefaultNetworkActive = true; + private volatile boolean mIsDefaultNetworkActive; private final ArrayMap mActiveIdleTimers = new ArrayMap<>(); + private final boolean mIsAtLeastU; private static class IdleTimerParams { public final int timeout; @@ -11141,10 +11141,11 @@ public class ConnectivityService extends IConnectivityManager.Stub } LegacyNetworkActivityTracker(@NonNull Context context, @NonNull INetd netd, - @NonNull Handler handler) { + @NonNull Handler handler, boolean isAtLeastU) { mContext = context; mNetd = netd; mHandler = handler; + mIsAtLeastU = isAtLeastU; } private void ensureRunningOnConnectivityServiceThread() { @@ -11227,8 +11228,10 @@ public class ConnectivityService extends IConnectivityManager.Stub * * Every {@code setupDataActivityTracking} should be paired with a * {@link #removeDataActivityTracking} for cleanup. + * + * @return true if the idleTimer is added to the network, false otherwise */ - private void setupDataActivityTracking(NetworkAgentInfo networkAgent) { + private boolean setupDataActivityTracking(NetworkAgentInfo networkAgent) { final String iface = networkAgent.linkProperties.getInterfaceName(); final int timeout; @@ -11247,23 +11250,22 @@ public class ConnectivityService extends IConnectivityManager.Stub 15); type = NetworkCapabilities.TRANSPORT_WIFI; } else { - return; // do not track any other networks + return false; // do not track any other networks } updateRadioPowerState(true /* isActive */, type); if (timeout > 0 && iface != null) { try { - // Networks start up. - mIsDefaultNetworkActive = true; mActiveIdleTimers.put(iface, new IdleTimerParams(timeout, type)); mNetd.idletimerAddInterface(iface, timeout, Integer.toString(type)); - reportNetworkActive(); + return true; } catch (Exception e) { // You shall not crash! loge("Exception in setupDataActivityTracking " + e); } } + return false; } /** @@ -11300,28 +11302,34 @@ public class ConnectivityService extends IConnectivityManager.Stub } } + private void updateDefaultNetworkActivity(NetworkAgentInfo defaultNetwork, + boolean hasIdleTimer) { + if (defaultNetwork != null) { + mIsDefaultNetworkActive = true; + // On T-, callbacks are called only when the network has the idle timer. + if (mIsAtLeastU || hasIdleTimer) { + reportNetworkActive(); + } + } else { + // If there is no default network, default network is considered inactive. + mIsDefaultNetworkActive = false; + } + } + /** * Update data activity tracking when network state is updated. */ public void updateDataActivityTracking(NetworkAgentInfo newNetwork, NetworkAgentInfo oldNetwork) { ensureRunningOnConnectivityServiceThread(); + boolean hasIdleTimer = false; if (newNetwork != null) { - setupDataActivityTracking(newNetwork); + hasIdleTimer = setupDataActivityTracking(newNetwork); } + updateDefaultNetworkActivity(newNetwork, hasIdleTimer); if (oldNetwork != null) { removeDataActivityTracking(oldNetwork); } - if (mActiveIdleTimers.isEmpty()) { - // If there are no idle timers, it means that system is not monitoring activity, - // so the default network is always considered active. - // - // TODO : Distinguish between the cases where mActiveIdleTimers is empty because - // tracking is disabled (negative idle timer value configured), or no active - // default network. In the latter case, this reports active but it should report - // inactive. - mIsDefaultNetworkActive = true; - } } private void updateRadioPowerState(boolean isActive, int transportType) { diff --git a/tests/unit/java/com/android/server/ConnectivityServiceTest.java b/tests/unit/java/com/android/server/ConnectivityServiceTest.java index 7d1871a66c..8de6a3181d 100755 --- a/tests/unit/java/com/android/server/ConnectivityServiceTest.java +++ b/tests/unit/java/com/android/server/ConnectivityServiceTest.java @@ -11309,18 +11309,17 @@ public class ConnectivityServiceTest { } @Test - public void testOnNetworkActive_NewEthernetConnects_CallbackNotCalled() throws Exception { - // LegacyNetworkActivityTracker calls onNetworkActive callback only for networks that + public void testOnNetworkActive_NewEthernetConnects_Callback() throws Exception { + // On T-, LegacyNetworkActivityTracker calls onNetworkActive callback only for networks that // tracker adds the idle timer to. And the tracker does not set the idle timer for the // ethernet network. // So onNetworkActive is not called when the ethernet becomes the default network - doTestOnNetworkActive_NewNetworkConnects(TRANSPORT_ETHERNET, false /* expectCallback */); + doTestOnNetworkActive_NewNetworkConnects(TRANSPORT_ETHERNET, mDeps.isAtLeastU()); } @Test public void testIsDefaultNetworkActiveNoDefaultNetwork() throws Exception { - // isDefaultNetworkActive returns true if there is no default network, which is known issue. - assertTrue(mCm.isDefaultNetworkActive()); + assertFalse(mCm.isDefaultNetworkActive()); final LinkProperties cellLp = new LinkProperties(); cellLp.setInterfaceName(MOBILE_IFNAME); @@ -11332,7 +11331,7 @@ public class ConnectivityServiceTest { mCellAgent.disconnect(); waitForIdle(); - assertTrue(mCm.isDefaultNetworkActive()); + assertFalse(mCm.isDefaultNetworkActive()); } @Test