diff --git a/library/common/src/main/java/com/google/android/exoplayer2/util/NetworkTypeObserver.java b/library/common/src/main/java/com/google/android/exoplayer2/util/NetworkTypeObserver.java index ff045a195b..457121abf1 100644 --- a/library/common/src/main/java/com/google/android/exoplayer2/util/NetworkTypeObserver.java +++ b/library/common/src/main/java/com/google/android/exoplayer2/util/NetworkTypeObserver.java @@ -25,8 +25,8 @@ import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.os.Handler; import android.os.Looper; -import android.telephony.PhoneStateListener; -import android.telephony.ServiceState; +import android.telephony.TelephonyCallback; +import android.telephony.TelephonyCallback.DisplayInfoListener; import android.telephony.TelephonyDisplayInfo; import android.telephony.TelephonyManager; import androidx.annotation.GuardedBy; @@ -58,24 +58,6 @@ public final class NetworkTypeObserver { void onNetworkTypeChanged(@C.NetworkType int networkType); } - /* - * Static configuration that may need to be set at app startup time is located in a separate - * static Config class. This allows apps to set their desired config without incurring unnecessary - * class loading costs during startup. - */ - /** Configuration for {@link NetworkTypeObserver}. */ - public static final class Config { - - private static volatile boolean disable5GNsaDisambiguation; - - /** Disables logic to disambiguate 5G-NSA networks from 4G networks. */ - public static void disable5GNsaDisambiguation() { - disable5GNsaDisambiguation = true; - } - - private Config() {} - } - @Nullable private static NetworkTypeObserver staticInstance; private final Handler mainHandler; @@ -231,26 +213,16 @@ public final class NetworkTypeObserver { @Override public void onReceive(Context context, Intent intent) { @C.NetworkType int networkType = getNetworkTypeFromConnectivityManager(context); - if (Util.SDK_INT >= 29 - && !Config.disable5GNsaDisambiguation - && networkType == C.NETWORK_TYPE_4G) { + if (Util.SDK_INT >= 31 && networkType == C.NETWORK_TYPE_4G) { // Delay update of the network type to check whether this is actually 5G-NSA. try { - // We can't access TelephonyManager getters like getServiceState() directly as they - // require special permissions. Attaching a listener is permission-free because the - // callback data is censored to not include sensitive information. TelephonyManager telephonyManager = checkNotNull((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE)); - TelephonyManagerListener listener = new TelephonyManagerListener(); - if (Util.SDK_INT < 31) { - telephonyManager.listen(listener, PhoneStateListener.LISTEN_SERVICE_STATE); - } else { - // Display info information can only be requested without permission from API 31. - telephonyManager.listen(listener, PhoneStateListener.LISTEN_DISPLAY_INFO_CHANGED); - } + DisplayInfoCallback callback = new DisplayInfoCallback(); + telephonyManager.registerTelephonyCallback(context.getMainExecutor(), callback); // We are only interested in the initial response with the current state, so unregister // the listener immediately. - telephonyManager.listen(listener, PhoneStateListener.LISTEN_NONE); + telephonyManager.unregisterTelephonyCallback(callback); return; } catch (RuntimeException e) { // Ignore problems with listener registration and keep reporting as 4G. @@ -260,19 +232,9 @@ public final class NetworkTypeObserver { } } - private class TelephonyManagerListener extends PhoneStateListener { + @RequiresApi(31) + private final class DisplayInfoCallback extends TelephonyCallback implements DisplayInfoListener { - @Override - public void onServiceStateChanged(@Nullable ServiceState serviceState) { - // This workaround to check the toString output of ServiceState only works on API 29 and 30. - String serviceStateString = serviceState == null ? "" : serviceState.toString(); - boolean is5gNsa = - serviceStateString.contains("nrState=CONNECTED") - || serviceStateString.contains("nrState=NOT_RESTRICTED"); - updateNetworkType(is5gNsa ? C.NETWORK_TYPE_5G_NSA : C.NETWORK_TYPE_4G); - } - - @RequiresApi(31) @Override public void onDisplayInfoChanged(TelephonyDisplayInfo telephonyDisplayInfo) { int overrideNetworkType = telephonyDisplayInfo.getOverrideNetworkType(); diff --git a/library/core/src/test/java/com/google/android/exoplayer2/upstream/DefaultBandwidthMeterTest.java b/library/core/src/test/java/com/google/android/exoplayer2/upstream/DefaultBandwidthMeterTest.java index 5bcd5f0f9f..9c277ca31a 100644 --- a/library/core/src/test/java/com/google/android/exoplayer2/upstream/DefaultBandwidthMeterTest.java +++ b/library/core/src/test/java/com/google/android/exoplayer2/upstream/DefaultBandwidthMeterTest.java @@ -25,7 +25,6 @@ import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.net.NetworkInfo.DetailedState; import android.net.Uri; -import android.telephony.ServiceState; import android.telephony.TelephonyDisplayInfo; import android.telephony.TelephonyManager; import androidx.test.core.app.ApplicationProvider; @@ -232,7 +231,7 @@ public final class DefaultBandwidthMeterTest { } @Test - @Config(minSdk = 29) // 5G detection support was added in API 29. + @Config(minSdk = 31) // 5G-NSA detection is supported from API 31. public void defaultInitialBitrateEstimate_for5gNsa_isGreaterThanEstimateFor4g() { setActiveNetworkInfo(networkInfo4g); DefaultBandwidthMeter bandwidthMeter4g = @@ -248,7 +247,7 @@ public final class DefaultBandwidthMeterTest { } @Test - @Config(minSdk = 29) // 5G detection support was added in API 29. + @Config(minSdk = 29) // 5G-SA detection is supported from API 29. public void defaultInitialBitrateEstimate_for5gSa_isGreaterThanEstimateFor3g() { setActiveNetworkInfo(networkInfo3g); DefaultBandwidthMeter bandwidthMeter3g = @@ -360,7 +359,7 @@ public final class DefaultBandwidthMeterTest { } @Test - @Config(minSdk = 29) // 5G detection support was added in API 29. + @Config(minSdk = 31) // 5G-NSA detection is supported from API 31. public void defaultInitialBitrateEstimate_for5gNsa_forFastCountry_isGreaterThanEstimateForSlowCountry() { setActiveNetworkInfo(networkInfo4g, TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA); @@ -378,7 +377,7 @@ public final class DefaultBandwidthMeterTest { } @Test - @Config(minSdk = 29) // 5G detection support was added in API 29. + @Config(minSdk = 29) // 5G-SA detection support was added in API 29. public void defaultInitialBitrateEstimate_for5gSa_forFastCountry_isGreaterThanEstimateForSlowCountry() { setActiveNetworkInfo(networkInfo5gSa); @@ -546,7 +545,7 @@ public final class DefaultBandwidthMeterTest { } @Test - @Config(minSdk = 29) // 5G detection support was added in API 29. + @Config(minSdk = 31) // 5G-NSA detection is supported from API 31. public void initialBitrateEstimateOverwrite_for5gNsa_whileConnectedTo5gNsa_setsInitialEstimate() { setActiveNetworkInfo(networkInfo4g, TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA); DefaultBandwidthMeter bandwidthMeter = @@ -559,7 +558,7 @@ public final class DefaultBandwidthMeterTest { } @Test - @Config(minSdk = 29) // 5G detection support was added in API 29. + @Config(minSdk = 31) // 5G-NSA detection is supported from API 31. public void initialBitrateEstimateOverwrite_for5gNsa_whileConnectedToOtherNetwork_doesNotSetInitialEstimate() { setActiveNetworkInfo(networkInfo4g); @@ -573,7 +572,7 @@ public final class DefaultBandwidthMeterTest { } @Test - @Config(minSdk = 29) // 5G detection support was added in API 29. + @Config(minSdk = 29) // 5G-SA detection is supported from API 29. public void initialBitrateEstimateOverwrite_for5gSa_whileConnectedTo5gSa_setsInitialEstimate() { setActiveNetworkInfo(networkInfo5gSa); DefaultBandwidthMeter bandwidthMeter = @@ -586,7 +585,7 @@ public final class DefaultBandwidthMeterTest { } @Test - @Config(minSdk = 29) // 5G detection support was added in API 29. + @Config(minSdk = 29) // 5G-SA detection is supported from API 29. public void initialBitrateEstimateOverwrite_for5gSa_whileConnectedToOtherNetwork_doesNotSetInitialEstimate() { setActiveNetworkInfo(networkInfoWifi); @@ -712,19 +711,6 @@ public final class DefaultBandwidthMeterTest { ShadowTelephonyManager.createTelephonyDisplayInfo( networkInfo.getType(), networkTypeOverride); Shadows.shadowOf(telephonyManager).setTelephonyDisplayInfo(displayInfo); - } else if (Util.SDK_INT >= 29) { - ServiceState serviceState = new ServiceState(); - if (networkTypeOverride == TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA) { - // Replicate known platform hack that includes special string indicating 5G-NSA. - serviceState = - new ServiceState() { - @Override - public String toString() { - return "...nrState=CONNECTED..."; - } - }; - } - Shadows.shadowOf(telephonyManager).setServiceState(serviceState); } // Create a sticky broadcast for the connectivity action because Robolectric isn't replying with // the current network state if a receiver for this intent is registered.