google / ExoPlayer

This project is deprecated and stale. The latest ExoPlayer code is available in https://github.com/androidx/media
https://developer.android.com/media/media3/exoplayer
Apache License 2.0
21.74k stars 6.03k forks source link

Re: SecurityException caused by ConnectivityManager.registerDefaultNetworkCallback #9776

Open NathanSass opened 2 years ago

NathanSass commented 2 years ago

This issue is a continuation of 9002. We are still getting the original crash posted in the issue, because the solution shipped is modification to Requirements.isInternetConnectivityValidated which is not in the original stack trace.

The fix in 9002 is for ConnectivityManager.getNetworkCapabilities but ConnectivityManager.registerDefaultNetworkCallback also throws that exception and it is not handled.

Caused by java.lang.SecurityException
Package android does not belong to 10308
android.os.Parcel.createExceptionOrNull (Parcel.java:2385)
android.os.Parcel.createException (Parcel.java:2369)
android.os.Parcel.readException (Parcel.java:2352)
android.os.Parcel.readException (Parcel.java:2294)
android.net.IConnectivityManager$Stub$Proxy.requestNetwork (IConnectivityManager.java:4597)
android.net.ConnectivityManager.sendRequestForNetwork (ConnectivityManager.java:4194)
android.net.ConnectivityManager.registerDefaultNetworkCallback (ConnectivityManager.java:4706)
android.net.ConnectivityManager.registerDefaultNetworkCallback (ConnectivityManager.java:4673)
com.google.android.exoplayer2.scheduler.RequirementsWatcher.registerNetworkCallbackV24 (RequirementsWatcher.java:136)
com.google.android.exoplayer2.scheduler.RequirementsWatcher.start (RequirementsWatcher.java:91)
com.google.android.exoplayer2.offline.DownloadManager.<init> (DownloadManager.java:278)
marcbaechinger commented 2 years ago

Thanks for reporting.

I think the fix we provided is probably much easier than what it needs here. There, we catch the SecurityException that is thrown by the system when we call getNetworkCapabilities() and then we assume that the internet connectivity is successfully validated. So on devices with this bug that causes throwing the SecurityException, this makes it work approximately the same as for API levels prior to 24 when no such a test was possible.

With the case of registerDefaultNetworkCallback/unregisterNetworkCallback it is more difficult I think, because the exception is raised when we try to register a network callback that is used to be aware of the network status to start/stop downloads. As far as I understand, we can not just switch to the alternative (< API 24) broadcast receiver on higher APIs because this has been deprecated in favor of the network callbacks of which registration throws the exception you are reporting. For recent APIs levels we can not expect that the alternative broadcast event is emitted.

We need to investigate what's best to do here and whether there are ways to avoid the crash and still have awareness of changes in the network availability to start/stop downloads accordingly.

Slecornu commented 2 years ago

Hey @marcbaechinger ,

I'm also experiencing this bug on Android 11, and I'm a bit worried considering the DownloadManager is required for downloading content but I'm unable to initialise it. I would have thought the inability to download on Android 11 would be fixed sooner, but perhaps I'm missing something!

Will the DownloadManager initialiser be fixed for Android 11?
Is there anything I can do in the meantime to work around this?

Looking forward to your response, thank you!