Add executing thread check in LegacyNetworkActivityTracker

One of the reasons for having locks in LegacyNetworkActivityTracker is
mNetworkActive is updated from non handler thread.
aosp/2606673 moves network activity change processing to handler thread.
All the methods that update mNetworkActive and mActiveIdleTimers should
be called from handler thread now.

Bug: 267870186
Bug: 279380356
Test: atest FrameworksNetTests
Change-Id: I5c8cd0b253a77a2740ade3a0145ef80336b3b133
This commit is contained in:
Motomu Utsumi
2023-05-30 19:24:31 +09:00
parent 00ccb51aa3
commit e0ecaea594

View File

@@ -1703,7 +1703,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
mUserAllContext.registerReceiver(mPackageIntentReceiver, packageIntentFilter,
null /* broadcastPermission */, mHandler);
mNetworkActivityTracker = new LegacyNetworkActivityTracker(mContext, mNetd);
mNetworkActivityTracker = new LegacyNetworkActivityTracker(mContext, mNetd, mHandler);
final NetdCallback netdCallback = new NetdCallback();
try {
@@ -11115,6 +11115,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
private static final int NO_UID = -1;
private final Context mContext;
private final INetd mNetd;
private final Handler mHandler;
private final RemoteCallbackList<INetworkActivityListener> mNetworkActivityListeners =
new RemoteCallbackList<>();
// Indicate the current system default network activity is active or not.
@@ -11133,12 +11134,22 @@ public class ConnectivityService extends IConnectivityManager.Stub
}
}
LegacyNetworkActivityTracker(@NonNull Context context, @NonNull INetd netd) {
LegacyNetworkActivityTracker(@NonNull Context context, @NonNull INetd netd,
@NonNull Handler handler) {
mContext = context;
mNetd = netd;
mHandler = handler;
}
private void ensureRunningOnConnectivityServiceThread() {
if (mHandler.getLooper().getThread() != Thread.currentThread()) {
throw new IllegalStateException("Not running on ConnectivityService thread: "
+ Thread.currentThread().getName());
}
}
public void handleReportNetworkActivity(NetworkActivityParams activityParams) {
ensureRunningOnConnectivityServiceThread();
sendDataActivityBroadcast(transportTypeToLegacyType(activityParams.label),
activityParams.isActive, activityParams.timestampNs);
synchronized (mActiveIdleTimers) {
@@ -11299,6 +11310,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
*/
public void updateDataActivityTracking(NetworkAgentInfo newNetwork,
NetworkAgentInfo oldNetwork) {
ensureRunningOnConnectivityServiceThread();
if (newNetwork != null) {
setupDataActivityTracking(newNetwork);
}