[Thread] post broacast receivers on the handler thread

It turns out context.registerReceiver(receiver, intentFilter,
permission, flags, handler) is not posting the receiver on the handler
and will result in ANR in activity manager if the receiver is blocked

This commit makes it safer by posting the receiver body to mHandler
explicitly.

Bug: 340368050
Change-Id: Ib6b8c0a8952ce43c935f39b0978f47268b108a79
This commit is contained in:
Kangping Dong 2024-06-22 16:56:47 +08:00
commit f8aaac0dd0
3 changed files with 6 additions and 11 deletions

View file

@ -523,12 +523,10 @@ final class ThreadNetworkControllerService extends IThreadNetworkController.Stub
new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
onUserRestrictionsChanged(isThreadUserRestricted());
mHandler.post(() -> onUserRestrictionsChanged(isThreadUserRestricted()));
}
},
new IntentFilter(UserManager.ACTION_USER_RESTRICTIONS_CHANGED),
null /* broadcastPermission */,
mHandler);
new IntentFilter(UserManager.ACTION_USER_RESTRICTIONS_CHANGED));
}
private void onUserRestrictionsChanged(boolean newUserRestrictedState) {
@ -580,12 +578,10 @@ final class ThreadNetworkControllerService extends IThreadNetworkController.Stub
new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
onAirplaneModeChanged(isAirplaneModeOn());
mHandler.post(() -> onAirplaneModeChanged(isAirplaneModeOn()));
}
},
new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED),
null /* broadcastPermission */,
mHandler);
new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED));
}
private void onAirplaneModeChanged(boolean newAirplaneModeOn) {

View file

@ -45,6 +45,7 @@ import static com.google.common.util.concurrent.MoreExecutors.directExecutor;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.fail;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import android.content.Context;

View file

@ -539,9 +539,7 @@ public final class ThreadNetworkControllerServiceTest {
.when(mContext)
.registerReceiver(
any(BroadcastReceiver.class),
argThat(actualIntentFilter -> actualIntentFilter.hasAction(action)),
any(),
any());
argThat(actualIntentFilter -> actualIntentFilter.hasAction(action)));
return receiverRef;
}