grpc-java icon indicating copy to clipboard operation
grpc-java copied to clipboard

android: fix network change handling on API levels 24+

Open dmytroreutov opened this issue 3 months ago • 2 comments

Relates to https://github.com/grpc/grpc-java/pull/12327

PS: Not using android.net.ConnectivityManager.NetworkCallback#onCapabilitiesChanged for API 24+ can also be an issue if capabilities change and user needs to receive updates.

This PR should improve network change detection for API levels 24+. For example, in case of WI-FI -> Cellular transition and vice versa, NetworkCapabilities will change it's transport, resulting in android.net.ConnectivityManager.NetworkCallback#onCapabilitiesChanged invocation. We can check, if NetworkCapabilities contain needed capabilities after such transition.

For more detailed network change detection, something similar to this can be done.

dmytroreutov avatar Sep 05 '25 16:09 dmytroreutov

Do we need something more sophisticated? In the Android docs:

Starting with Build.VERSION_CODES.O this method is guaranteed to be called immediately after onAvailable(Network).

So we'll end up doing enterIdle() twice. If an RPC is attempted in between, that may mean we do a DNS request and throw it away, which we might just accept. If we're saying that's okay then at least we should have a comment.

ejona86 avatar Sep 18 '25 19:09 ejona86

@ejona86 this is up to debate (maybe).

During my test on a real device adding android.net.ConnectivityManager.NetworkCallback#onCapabilitiesChanged to AndroidChannel was helpful, because network transport switch triggers this method and AndroidChannel can already validate NetworkCapabilities there.

IIRC, switch from Wi-Fi to cellular (when .enterIdle() is finally invoked) will be handled after a couple of seconds with current implementation. Cellular to Wi-Fi is processed much faster.

I can agree that this case is kinda niche and less frustrating than on API levels < 24. That's why I opened a separate PR and initially doubted it's importance.

dmytroreutov avatar Sep 18 '25 20:09 dmytroreutov