Skip to content

Commit

Permalink
Network type cleanup
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ojw28 committed Oct 18, 2018
1 parent 6e7b415 commit dbf5b07
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 18 deletions.
2 changes: 2 additions & 0 deletions library/core/src/androidTest/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
xmlns:tools="http://schemas.android.com/tools"
package="com.google.android.exoplayer2.core.test">

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

<application android:debuggable="true"
android:allowBackup="false"
tools:ignore="MissingApplicationIcon,HardcodedDebugMode">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,9 @@ public Builder setClock(Clock clock) {
* @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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1620,31 +1620,24 @@ public static int crc(byte[] bytes, int start, int end, int initialValue) {
}

/**
* 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ public ExoPlayerTestRunner build(Context context) {
trackSelector = new DefaultTrackSelector();
}
if (bandwidthMeter == null) {
bandwidthMeter = new DefaultBandwidthMeter.Builder().build();
bandwidthMeter = new DefaultBandwidthMeter.Builder(context).build();
}
if (renderersFactory == null) {
if (renderers == null) {
Expand Down

0 comments on commit dbf5b07

Please sign in to comment.