Fixes isDefaultNetworkActive and onNetworkActive unreasonable behavior
This CL changes the behavior as follows.
Before this CL, isDefaultNetworkActive returns true if there is no
default network.
After this CL, isDefaultNetworkActive returns false if there is no
default network on all releases.
Before this CL, LegacyNetworkActivityTracker does not call
onNetworkActive callback for networks that tracker does not add the idle
timer to (e.g. ethernet, cell network configured with activity tracking
disabled).
After this CL,
On T-, onNetworkActive keeps the current behavior.
On U+, onNetworkActive is called regardless of the idle timer existence. This behavior is consistent with ConnectivityManager#isDefaultNetworkActive, which always returns true for a network that has no activity tracking.
Bug: 267870186
Bug: 279380356
Test: atest FrameworksNetTests
Change-Id: I7b1d493fea0ab028b53a3d640a58a00ebbdcc143
diff --git a/service/src/com/android/server/ConnectivityService.java b/service/src/com/android/server/ConnectivityService.java
index ba1327d..f16ce12 100755
--- a/service/src/com/android/server/ConnectivityService.java
+++ b/service/src/com/android/server/ConnectivityService.java
@@ -1703,7 +1703,8 @@
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 {
@@ -11120,10 +11121,9 @@
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<String, IdleTimerParams> mActiveIdleTimers = new ArrayMap<>();
+ private final boolean mIsAtLeastU;
private static class IdleTimerParams {
public final int timeout;
@@ -11136,10 +11136,11 @@
}
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() {
@@ -11222,8 +11223,10 @@
*
* 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;
@@ -11242,23 +11245,22 @@
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;
}
/**
@@ -11295,28 +11297,34 @@
}
}
+ 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 fd30c08..558d327 100755
--- a/tests/unit/java/com/android/server/ConnectivityServiceTest.java
+++ b/tests/unit/java/com/android/server/ConnectivityServiceTest.java
@@ -11315,18 +11315,17 @@
}
@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);
@@ -11338,7 +11337,7 @@
mCellAgent.disconnect();
waitForIdle();
- assertTrue(mCm.isDefaultNetworkActive());
+ assertFalse(mCm.isDefaultNetworkActive());
}
@Test