Skip to content

Commit dc31c22

Browse files
committed
fix(android): Report 5G mmWave and keep the callback on failure
The NSA mmWave override network type fell through to the network type, which is LTE for NSA, so a 5G mmWave connection reported 4g. Unregistering also dropped the callback reference before it was unregistered, orphaning a still-registered callback when the telephony manager was gone.
1 parent c56405c commit dc31c22

2 files changed

Lines changed: 46 additions & 3 deletions

File tree

‎sentry-android-core/src/main/java/io/sentry/android/core/internal/util/CellularNetworkTechnologyProvider.java‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,20 +111,20 @@ public void register() {
111111
/** Stops listening for display info changes and forgets the last reported technology. */
112112
@SuppressLint("NewApi")
113113
public void unregister() {
114-
final @Nullable Object callback;
115114
try (final @NotNull ISentryLifecycleToken ignored = lock.acquire()) {
116-
callback = displayInfoCallback;
117-
displayInfoCallback = null;
115+
final @Nullable Object callback = displayInfoCallback;
118116
displayInfoTechnology = null;
119117
if (callback == null) {
120118
return;
121119
}
122120
final @Nullable TelephonyManager telephonyManager = getTelephonyManager();
123121
if (telephonyManager == null) {
122+
// Keep the reference so a later unregister can still reach the registered callback.
124123
return;
125124
}
126125
try {
127126
telephonyManager.unregisterTelephonyCallback((TelephonyCallback) callback);
127+
displayInfoCallback = null;
128128
logger.log(SentryLevel.DEBUG, "Stopped listening for cellular network technology changes.");
129129
} catch (SecurityException | IllegalStateException | UnsupportedOperationException e) {
130130
logger.log(
@@ -176,10 +176,14 @@ public void unregister() {
176176
* device showing 5G to the user is also reported as 5G.
177177
*/
178178
@RequiresApi(api = Build.VERSION_CODES.S)
179+
@SuppressWarnings("deprecation")
179180
static @Nullable String toGeneration(final @NotNull TelephonyDisplayInfo displayInfo) {
180181
switch (displayInfo.getOverrideNetworkType()) {
181182
case TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA:
182183
case TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED:
184+
// Deprecated in favour of NR_ADVANCED, but still reported on Android 11. Its underlying
185+
// network type is LTE, so without this case a 5G mmWave connection reports 4g.
186+
case TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA_MMWAVE:
183187
return GENERATION_5G;
184188
case TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_LTE_CA:
185189
case TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_LTE_ADVANCED_PRO:

‎sentry-android-core/src/test/java/io/sentry/android/core/internal/util/CellularNetworkTechnologyProviderTest.kt‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,45 @@ class CellularNetworkTechnologyProviderTest {
156156
assertThat(CellularNetworkTechnologyProvider.toGeneration(displayInfo)).isEqualTo("5g")
157157
}
158158

159+
@Test
160+
fun `toGeneration maps the deprecated NSA mmWave override to 5g`() {
161+
// Its underlying network type is LTE, so falling through to the network type reports 4g.
162+
val displayInfo =
163+
fixture.displayInfo(
164+
networkType = TelephonyManager.NETWORK_TYPE_LTE,
165+
overrideNetworkType = TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA_MMWAVE,
166+
)
167+
168+
assertThat(CellularNetworkTechnologyProvider.toGeneration(displayInfo)).isEqualTo("5g")
169+
}
170+
171+
@Test
172+
fun `toGeneration maps the NR advanced override to 5g`() {
173+
val displayInfo =
174+
fixture.displayInfo(
175+
networkType = TelephonyManager.NETWORK_TYPE_LTE,
176+
overrideNetworkType = TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
177+
)
178+
179+
assertThat(CellularNetworkTechnologyProvider.toGeneration(displayInfo)).isEqualTo("5g")
180+
}
181+
182+
@Test
183+
fun `unregistering without a telephony manager keeps the callback for a later attempt`() {
184+
val provider = fixture.getSut(sdkVersion = Build.VERSION_CODES.S)
185+
val listener = fixture.registerAndCaptureCallback(provider)
186+
// The telephony manager disappearing must not orphan the still-registered callback.
187+
whenever(fixture.context.getSystemService(eq(Context.TELEPHONY_SERVICE))).thenReturn(null)
188+
189+
provider.unregister()
190+
191+
whenever(fixture.context.getSystemService(eq(Context.TELEPHONY_SERVICE)))
192+
.thenReturn(fixture.telephonyManager)
193+
provider.unregister()
194+
195+
verify(fixture.telephonyManager).unregisterTelephonyCallback(listener as TelephonyCallback)
196+
}
197+
159198
@Test
160199
fun `toGeneration falls back to the network type without an override`() {
161200
val displayInfo =

0 commit comments

Comments
 (0)