Wrap TelephonyCallback in nested class to avoid class loading issues.

PiperOrigin-RevId: 450643824
This commit is contained in:
tonihei 2022-05-24 10:33:59 +00:00 committed by Marc Baechinger
parent 196a99aa5f
commit 9463efef4b

View File

@ -216,34 +216,49 @@ public final class NetworkTypeObserver {
@C.NetworkType int networkType = getNetworkTypeFromConnectivityManager(context); @C.NetworkType int networkType = getNetworkTypeFromConnectivityManager(context);
if (Util.SDK_INT >= 31 && 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. // Delay update of the network type to check whether this is actually 5G-NSA.
try { Api31.disambiguate4gAnd5gNsa(context, /* instance= */ NetworkTypeObserver.this);
TelephonyManager telephonyManager = } else {
checkNotNull((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE)); updateNetworkType(networkType);
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.unregisterTelephonyCallback(callback);
return;
} catch (RuntimeException e) {
// Ignore problems with listener registration and keep reporting as 4G.
}
} }
updateNetworkType(networkType);
} }
} }
@RequiresApi(31) @RequiresApi(31)
private final class DisplayInfoCallback extends TelephonyCallback implements DisplayInfoListener { private static final class Api31 {
@Override public static void disambiguate4gAnd5gNsa(Context context, NetworkTypeObserver instance) {
public void onDisplayInfoChanged(TelephonyDisplayInfo telephonyDisplayInfo) { try {
int overrideNetworkType = telephonyDisplayInfo.getOverrideNetworkType(); TelephonyManager telephonyManager =
boolean is5gNsa = checkNotNull((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE));
overrideNetworkType == TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA DisplayInfoCallback callback = new DisplayInfoCallback(instance);
|| overrideNetworkType == TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA_MMWAVE telephonyManager.registerTelephonyCallback(context.getMainExecutor(), callback);
|| overrideNetworkType == TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED; // We are only interested in the initial response with the current state, so unregister
updateNetworkType(is5gNsa ? C.NETWORK_TYPE_5G_NSA : C.NETWORK_TYPE_4G); // the listener immediately.
telephonyManager.unregisterTelephonyCallback(callback);
} catch (RuntimeException e) {
// Ignore problems with listener registration and keep reporting as 4G.
instance.updateNetworkType(C.NETWORK_TYPE_4G);
}
}
private static final class DisplayInfoCallback extends TelephonyCallback
implements DisplayInfoListener {
private final NetworkTypeObserver instance;
public DisplayInfoCallback(NetworkTypeObserver instance) {
this.instance = instance;
}
@Override
public void onDisplayInfoChanged(TelephonyDisplayInfo telephonyDisplayInfo) {
int overrideNetworkType = telephonyDisplayInfo.getOverrideNetworkType();
boolean is5gNsa =
overrideNetworkType == TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA
|| overrideNetworkType == TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA_MMWAVE
|| overrideNetworkType == TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED;
instance.updateNetworkType(is5gNsa ? C.NETWORK_TYPE_5G_NSA : C.NETWORK_TYPE_4G);
}
} }
} }
} }