Run callbacks on ConnectivityService thread
Run MultinetworkPolicyTracker and DataConnectionStats callbacks on the ConnectivityService handler thread. Previously the callbacks would be using the SystemServer foreground thread (Looper.myLooper()), or the broadcast thread for the MultinetworkPolicyTracker BroadcastReceiver. This is error-prone, can cause threading issues and makes it difficult to test the components. Test: atest FrameworksNetTests Change-Id: I189213dd363004abed294659165bf5430d153bba
This commit is contained in:
@@ -64,7 +64,7 @@ public class MultinetworkPolicyTracker {
|
|||||||
|
|
||||||
private final Context mContext;
|
private final Context mContext;
|
||||||
private final Handler mHandler;
|
private final Handler mHandler;
|
||||||
private final Runnable mReevaluateRunnable;
|
private final Runnable mAvoidBadWifiCallback;
|
||||||
private final List<Uri> mSettingsUris;
|
private final List<Uri> mSettingsUris;
|
||||||
private final ContentResolver mResolver;
|
private final ContentResolver mResolver;
|
||||||
private final SettingObserver mSettingObserver;
|
private final SettingObserver mSettingObserver;
|
||||||
@@ -81,12 +81,7 @@ public class MultinetworkPolicyTracker {
|
|||||||
public MultinetworkPolicyTracker(Context ctx, Handler handler, Runnable avoidBadWifiCallback) {
|
public MultinetworkPolicyTracker(Context ctx, Handler handler, Runnable avoidBadWifiCallback) {
|
||||||
mContext = ctx;
|
mContext = ctx;
|
||||||
mHandler = handler;
|
mHandler = handler;
|
||||||
mReevaluateRunnable = () -> {
|
mAvoidBadWifiCallback = avoidBadWifiCallback;
|
||||||
if (updateAvoidBadWifi() && avoidBadWifiCallback != null) {
|
|
||||||
avoidBadWifiCallback.run();
|
|
||||||
}
|
|
||||||
updateMeteredMultipathPreference();
|
|
||||||
};
|
|
||||||
mSettingsUris = Arrays.asList(
|
mSettingsUris = Arrays.asList(
|
||||||
Settings.Global.getUriFor(NETWORK_AVOID_BAD_WIFI),
|
Settings.Global.getUriFor(NETWORK_AVOID_BAD_WIFI),
|
||||||
Settings.Global.getUriFor(NETWORK_METERED_MULTIPATH_PREFERENCE));
|
Settings.Global.getUriFor(NETWORK_METERED_MULTIPATH_PREFERENCE));
|
||||||
@@ -95,15 +90,15 @@ public class MultinetworkPolicyTracker {
|
|||||||
mBroadcastReceiver = new BroadcastReceiver() {
|
mBroadcastReceiver = new BroadcastReceiver() {
|
||||||
@Override
|
@Override
|
||||||
public void onReceive(Context context, Intent intent) {
|
public void onReceive(Context context, Intent intent) {
|
||||||
reevaluate();
|
reevaluateInternal();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
TelephonyManager.from(ctx).listen(new PhoneStateListener() {
|
TelephonyManager.from(ctx).listen(new PhoneStateListener(handler.getLooper()) {
|
||||||
@Override
|
@Override
|
||||||
public void onActiveDataSubscriptionIdChanged(int subId) {
|
public void onActiveDataSubscriptionIdChanged(int subId) {
|
||||||
mActiveSubId = subId;
|
mActiveSubId = subId;
|
||||||
reevaluate();
|
reevaluateInternal();
|
||||||
}
|
}
|
||||||
}, PhoneStateListener.LISTEN_ACTIVE_DATA_SUBSCRIPTION_ID_CHANGE);
|
}, PhoneStateListener.LISTEN_ACTIVE_DATA_SUBSCRIPTION_ID_CHANGE);
|
||||||
|
|
||||||
@@ -119,7 +114,7 @@ public class MultinetworkPolicyTracker {
|
|||||||
final IntentFilter intentFilter = new IntentFilter();
|
final IntentFilter intentFilter = new IntentFilter();
|
||||||
intentFilter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
|
intentFilter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
|
||||||
mContext.registerReceiverAsUser(
|
mContext.registerReceiverAsUser(
|
||||||
mBroadcastReceiver, UserHandle.ALL, intentFilter, null, null);
|
mBroadcastReceiver, UserHandle.ALL, intentFilter, null, mHandler);
|
||||||
|
|
||||||
reevaluate();
|
reevaluate();
|
||||||
}
|
}
|
||||||
@@ -164,7 +159,17 @@ public class MultinetworkPolicyTracker {
|
|||||||
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
public void reevaluate() {
|
public void reevaluate() {
|
||||||
mHandler.post(mReevaluateRunnable);
|
mHandler.post(this::reevaluateInternal);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reevaluate the settings. Must be called on the handler thread.
|
||||||
|
*/
|
||||||
|
private void reevaluateInternal() {
|
||||||
|
if (updateAvoidBadWifi() && mAvoidBadWifiCallback != null) {
|
||||||
|
mAvoidBadWifiCallback.run();
|
||||||
|
}
|
||||||
|
updateMeteredMultipathPreference();
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean updateAvoidBadWifi() {
|
public boolean updateAvoidBadWifi() {
|
||||||
|
|||||||
@@ -1101,7 +1101,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
|
|||||||
mSettingsObserver = new SettingsObserver(mContext, mHandler);
|
mSettingsObserver = new SettingsObserver(mContext, mHandler);
|
||||||
registerSettingsCallbacks();
|
registerSettingsCallbacks();
|
||||||
|
|
||||||
final DataConnectionStats dataConnectionStats = new DataConnectionStats(mContext);
|
final DataConnectionStats dataConnectionStats = new DataConnectionStats(mContext, mHandler);
|
||||||
dataConnectionStats.startMonitoring();
|
dataConnectionStats.startMonitoring();
|
||||||
|
|
||||||
mKeepaliveTracker = new KeepaliveTracker(mContext, mHandler);
|
mKeepaliveTracker = new KeepaliveTracker(mContext, mHandler);
|
||||||
|
|||||||
Reference in New Issue
Block a user