Skip to content

Commit d035a85

Browse files
committed
fix(android): Keep the last cellular technology after backgrounding
Monitoring stops when the app goes to the background, and from API 31 on the display info listener is the only source, so clearing the cached generation left every background event without a technology while the connection type still resolved.
1 parent dc31c22 commit d035a85

2 files changed

Lines changed: 27 additions & 4 deletions

File tree

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,18 @@ public void register() {
108108
}
109109
}
110110

111-
/** Stops listening for display info changes and forgets the last reported technology. */
111+
/**
112+
* Stops listening for display info changes.
113+
*
114+
* <p>The last reported technology is kept. Monitoring stops when the app goes to the background,
115+
* and from API 31 on the listener is the only source, so discarding it would leave every
116+
* background event without a technology even though the connection type still resolves. It is
117+
* only ever paired with a connection that is cellular at the time the event is captured.
118+
*/
112119
@SuppressLint("NewApi")
113120
public void unregister() {
114121
try (final @NotNull ISentryLifecycleToken ignored = lock.acquire()) {
115122
final @Nullable Object callback = displayInfoCallback;
116-
displayInfoTechnology = null;
117123
if (callback == null) {
118124
return;
119125
}

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ class CellularNetworkTechnologyProviderTest {
271271
}
272272

273273
@Test
274-
fun `unregistering stops the listener and forgets the technology`() {
274+
fun `unregistering stops the listener`() {
275275
val provider = fixture.getSut(sdkVersion = Build.VERSION_CODES.S)
276276
val listener = fixture.registerAndCaptureCallback(provider)
277277
listener.onDisplayInfoChanged(
@@ -284,7 +284,24 @@ class CellularNetworkTechnologyProviderTest {
284284
provider.unregister()
285285

286286
verify(fixture.telephonyManager).unregisterTelephonyCallback(listener as TelephonyCallback)
287-
assertThat(provider.cellularNetworkTechnology).isNull()
287+
}
288+
289+
@Test
290+
fun `unregistering keeps the last reported technology`() {
291+
// Monitoring stops when the app backgrounds, and from API 31 the listener is the only source,
292+
// so dropping the value would leave background events without a technology.
293+
val provider = fixture.getSut(sdkVersion = Build.VERSION_CODES.S)
294+
val listener = fixture.registerAndCaptureCallback(provider)
295+
listener.onDisplayInfoChanged(
296+
fixture.displayInfo(
297+
networkType = TelephonyManager.NETWORK_TYPE_NR,
298+
overrideNetworkType = TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE,
299+
)
300+
)
301+
302+
provider.unregister()
303+
304+
assertThat(provider.cellularNetworkTechnology).isEqualTo("5g")
288305
}
289306

290307
@Test

0 commit comments

Comments
 (0)