Commit dbf5b073 by olly Committed by Oliver Woodman

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
parent 6e7b4151
...@@ -18,6 +18,8 @@ ...@@ -18,6 +18,8 @@
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
package="com.google.android.exoplayer2.core.test"> package="com.google.android.exoplayer2.core.test">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application android:debuggable="true" <application android:debuggable="true"
android:allowBackup="false" android:allowBackup="false"
tools:ignore="MissingApplicationIcon,HardcodedDebugMode"> tools:ignore="MissingApplicationIcon,HardcodedDebugMode">
......
...@@ -187,7 +187,9 @@ public final class DefaultBandwidthMeter implements BandwidthMeter, TransferList ...@@ -187,7 +187,9 @@ public final class DefaultBandwidthMeter implements BandwidthMeter, TransferList
* @return A bandwidth meter with the configured properties. * @return A bandwidth meter with the configured properties.
*/ */
public DefaultBandwidthMeter build() { 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) { if (initialBitrateEstimate == null) {
initialBitrateEstimate = initialBitrateEstimates.get(C.NETWORK_TYPE_UNKNOWN); initialBitrateEstimate = initialBitrateEstimates.get(C.NETWORK_TYPE_UNKNOWN);
} }
......
...@@ -1620,31 +1620,24 @@ public final class Util { ...@@ -1620,31 +1620,24 @@ public final class Util {
} }
/** /**
* Returns the {@link C.NetworkType} of the current network connection. {@link * Returns the {@link C.NetworkType} of the current network connection.
* 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.
* *
* @param context A context to access the connectivity manager. * @param context A context to access the connectivity manager.
* @return The {@link C.NetworkType} of the current network connection, or {@link * @return The {@link C.NetworkType} of the current network connection.
* C#NETWORK_TYPE_UNKNOWN} if the {@code ACCESS_NETWORK_STATE} permission is not granted or
* {@code context} is null.
*/ */
public static @C.NetworkType int getNetworkType(@Nullable Context context) { @C.NetworkType
public static int getNetworkType(Context context) {
if (context == null) { if (context == null) {
// Note: This is for backward compatibility only (context used to be @Nullable).
return C.NETWORK_TYPE_UNKNOWN; return C.NETWORK_TYPE_UNKNOWN;
} }
NetworkInfo networkInfo; NetworkInfo networkInfo;
try {
ConnectivityManager connectivityManager = ConnectivityManager connectivityManager =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivityManager == null) { if (connectivityManager == null) {
return C.NETWORK_TYPE_UNKNOWN; return C.NETWORK_TYPE_UNKNOWN;
} }
networkInfo = connectivityManager.getActiveNetworkInfo(); networkInfo = connectivityManager.getActiveNetworkInfo();
} catch (SecurityException e) {
// Permission ACCESS_NETWORK_STATE not granted.
return C.NETWORK_TYPE_UNKNOWN;
}
if (networkInfo == null || !networkInfo.isConnected()) { if (networkInfo == null || !networkInfo.isConnected()) {
return C.NETWORK_TYPE_OFFLINE; return C.NETWORK_TYPE_OFFLINE;
} }
......
...@@ -287,7 +287,7 @@ public final class ExoPlayerTestRunner implements Player.EventListener, ActionSc ...@@ -287,7 +287,7 @@ public final class ExoPlayerTestRunner implements Player.EventListener, ActionSc
trackSelector = new DefaultTrackSelector(); trackSelector = new DefaultTrackSelector();
} }
if (bandwidthMeter == null) { if (bandwidthMeter == null) {
bandwidthMeter = new DefaultBandwidthMeter.Builder().build(); bandwidthMeter = new DefaultBandwidthMeter.Builder(context).build();
} }
if (renderersFactory == null) { if (renderersFactory == null) {
if (renderers == null) { if (renderers == null) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment