From dbf5b073a60b2e6224e3d2275981ee915c731abc Mon Sep 17 00:00:00 2001 From: olly Date: Mon, 15 Oct 2018 08:30:28 -0700 Subject: [PATCH] Network type cleanup 1. Remove option to pass null context to Util.getNetworkType. IMO it's clearer for the caller to just not call the method in this case. 2. Stop using deprecated DefaultBandwidthMeter.Builder constructor in all but one place. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=217147091 --- .../core/src/androidTest/AndroidManifest.xml | 2 ++ .../upstream/DefaultBandwidthMeter.java | 4 ++- .../google/android/exoplayer2/util/Util.java | 25 +++++++------------ .../testutil/ExoPlayerTestRunner.java | 2 +- 4 files changed, 15 insertions(+), 18 deletions(-) diff --git a/library/core/src/androidTest/AndroidManifest.xml b/library/core/src/androidTest/AndroidManifest.xml index d9104b1077..c3c0852b29 100644 --- a/library/core/src/androidTest/AndroidManifest.xml +++ b/library/core/src/androidTest/AndroidManifest.xml @@ -18,6 +18,8 @@ xmlns:tools="http://schemas.android.com/tools" package="com.google.android.exoplayer2.core.test"> + + diff --git a/library/core/src/main/java/com/google/android/exoplayer2/upstream/DefaultBandwidthMeter.java b/library/core/src/main/java/com/google/android/exoplayer2/upstream/DefaultBandwidthMeter.java index e9f70ec92a..21e2ed4f65 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/upstream/DefaultBandwidthMeter.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/upstream/DefaultBandwidthMeter.java @@ -187,7 +187,9 @@ public final class DefaultBandwidthMeter implements BandwidthMeter, TransferList * @return A bandwidth meter with the configured properties. */ public DefaultBandwidthMeter build() { - Long initialBitrateEstimate = initialBitrateEstimates.get(Util.getNetworkType(context)); + Long initialBitrateEstimate = + initialBitrateEstimates.get( + context == null ? C.NETWORK_TYPE_UNKNOWN : Util.getNetworkType(context)); if (initialBitrateEstimate == null) { initialBitrateEstimate = initialBitrateEstimates.get(C.NETWORK_TYPE_UNKNOWN); } 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 2d3a0aeb78..116ad860e0 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 @@ -1620,31 +1620,24 @@ public final class Util { } /** - * Returns the {@link C.NetworkType} of the current network connection. {@link - * C#NETWORK_TYPE_UNKNOWN} will be returned if the {@code ACCESS_NETWORK_STATE} permission is not - * granted or the network connection type couldn't be determined. + * Returns the {@link C.NetworkType} of the current network connection. * * @param context A context to access the connectivity manager. - * @return The {@link C.NetworkType} of the current network connection, or {@link - * C#NETWORK_TYPE_UNKNOWN} if the {@code ACCESS_NETWORK_STATE} permission is not granted or - * {@code context} is null. + * @return The {@link C.NetworkType} of the current network connection. */ - public static @C.NetworkType int getNetworkType(@Nullable Context context) { + @C.NetworkType + public static int getNetworkType(Context context) { if (context == null) { + // Note: This is for backward compatibility only (context used to be @Nullable). return C.NETWORK_TYPE_UNKNOWN; } NetworkInfo networkInfo; - try { - ConnectivityManager connectivityManager = - (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); - if (connectivityManager == null) { - return C.NETWORK_TYPE_UNKNOWN; - } - networkInfo = connectivityManager.getActiveNetworkInfo(); - } catch (SecurityException e) { - // Permission ACCESS_NETWORK_STATE not granted. + ConnectivityManager connectivityManager = + (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); + if (connectivityManager == null) { return C.NETWORK_TYPE_UNKNOWN; } + networkInfo = connectivityManager.getActiveNetworkInfo(); if (networkInfo == null || !networkInfo.isConnected()) { return C.NETWORK_TYPE_OFFLINE; } diff --git a/testutils/src/main/java/com/google/android/exoplayer2/testutil/ExoPlayerTestRunner.java b/testutils/src/main/java/com/google/android/exoplayer2/testutil/ExoPlayerTestRunner.java index b613f7f364..401a2fae1b 100644 --- a/testutils/src/main/java/com/google/android/exoplayer2/testutil/ExoPlayerTestRunner.java +++ b/testutils/src/main/java/com/google/android/exoplayer2/testutil/ExoPlayerTestRunner.java @@ -287,7 +287,7 @@ public final class ExoPlayerTestRunner implements Player.EventListener, ActionSc trackSelector = new DefaultTrackSelector(); } if (bandwidthMeter == null) { - bandwidthMeter = new DefaultBandwidthMeter.Builder().build(); + bandwidthMeter = new DefaultBandwidthMeter.Builder(context).build(); } if (renderersFactory == null) { if (renderers == null) {