From 0a74e36f7b00aacfb0110817061aed978b4dcddf Mon Sep 17 00:00:00 2001 From: tonihei Date: Tue, 18 Jun 2019 11:31:54 +0100 Subject: [PATCH] Gracefully handle revoked ACCESS_NETWORK_STATE permission. This permission has normal access right and can't be revoked by the user. However, an app can choose to revoke it when using ExoPlayer, e.g. if no network is required and the app doesn't want to list this permission. Support this use case by gracefully catching the exception in the relevant places. Issue:#6019 PiperOrigin-RevId: 253759332 --- RELEASENOTES.md | 2 ++ .../google/android/exoplayer2/scheduler/Requirements.java | 3 ++- .../android/exoplayer2/scheduler/RequirementsWatcher.java | 3 ++- .../main/java/com/google/android/exoplayer2/util/Util.java | 7 ++++++- 4 files changed, 12 insertions(+), 3 deletions(-) diff --git a/RELEASENOTES.md b/RELEASENOTES.md index 4f75b95663..3ebdc01762 100644 --- a/RELEASENOTES.md +++ b/RELEASENOTES.md @@ -21,6 +21,8 @@ renderer error. * Add support for auto-detecting UDP streams in `DefaultDataSource` ([#6036](https://github.com/google/ExoPlayer/pull/6036)). +* Gracefully handle revoked `ACCESS_NETWORK_STATE` permission + ([#6019](https://github.com/google/ExoPlayer/issues/6019)). ### 2.10.2 ### diff --git a/library/core/src/main/java/com/google/android/exoplayer2/scheduler/Requirements.java b/library/core/src/main/java/com/google/android/exoplayer2/scheduler/Requirements.java index 882d9def3c..35f8e37dcf 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/scheduler/Requirements.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/scheduler/Requirements.java @@ -28,6 +28,7 @@ import android.os.Parcelable; import android.os.PowerManager; import androidx.annotation.IntDef; import androidx.annotation.Nullable; +import com.google.android.exoplayer2.util.Assertions; import com.google.android.exoplayer2.util.Util; import java.lang.annotation.Documented; import java.lang.annotation.Retention; @@ -129,7 +130,7 @@ public final class Requirements implements Parcelable { ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); - NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo(); + NetworkInfo networkInfo = Assertions.checkNotNull(connectivityManager).getActiveNetworkInfo(); if (networkInfo == null || !networkInfo.isConnected() || !isInternetConnectivityValidated(connectivityManager)) { diff --git a/library/core/src/main/java/com/google/android/exoplayer2/scheduler/RequirementsWatcher.java b/library/core/src/main/java/com/google/android/exoplayer2/scheduler/RequirementsWatcher.java index b9cbf681b1..0d9b8261d9 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/scheduler/RequirementsWatcher.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/scheduler/RequirementsWatcher.java @@ -128,7 +128,8 @@ public final class RequirementsWatcher { @TargetApi(23) private void registerNetworkCallbackV23() { ConnectivityManager connectivityManager = - (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); + Assertions.checkNotNull( + (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)); NetworkRequest request = new NetworkRequest.Builder() .addCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED) diff --git a/library/core/src/main/java/com/google/android/exoplayer2/util/Util.java b/library/core/src/main/java/com/google/android/exoplayer2/util/Util.java index 4dfb8b50d5..32e9c32a53 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/util/Util.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/util/Util.java @@ -1728,7 +1728,12 @@ public final class Util { if (connectivityManager == null) { return C.NETWORK_TYPE_UNKNOWN; } - networkInfo = connectivityManager.getActiveNetworkInfo(); + try { + networkInfo = connectivityManager.getActiveNetworkInfo(); + } catch (SecurityException e) { + // Expected if permission was revoked. + return C.NETWORK_TYPE_UNKNOWN; + } if (networkInfo == null || !networkInfo.isConnected()) { return C.NETWORK_TYPE_OFFLINE; }