Skip to content

Commit c58f4a4

Browse files
fix(android): Only use the Handler overload of registerDefaultNetworkCallback on API 26+ (#6634)
* fix(android): Only use the Handler overload of registerDefaultNetworkCallback on API 26+ Co-Authored-By: Nelson Osacky <nelson.osacky@sentry.io> * changelog Co-Authored-By: Nelson Osacky <nelson.osacky@sentry.io> --------- Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com> Co-authored-by: Nelson Osacky <nelson.osacky@sentry.io>
1 parent 7eda470 commit c58f4a4

3 files changed

Lines changed: 25 additions & 1 deletion

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
- Session Replay: Send Ktor client network request start and end times in milliseconds, so the requests line up with the replay ([#6619](https://github.com/getsentry/sentry-java/pull/6619))
2727
- Nav3: Stop attributing errors and spans from an Activity or ViewPager2 page without a `SentryNavEffect` to the most recently visited Nav3 root ([#6483](https://github.com/getsentry/sentry-java/pull/6483))
2828
- Android: Count deep sleep in breadcrumb, screenshot and view hierarchy debounce windows, so the first low-memory or battery breadcrumb after a long Doze is no longer dropped ([#6590](https://github.com/getsentry/sentry-java/pull/6590))
29+
- Android: Register network callbacks on API 24–25 when `AndroidConnectionStatusProvider` is created with a custom `Handler`, instead of failing because the `Handler` overload needs API 26 ([#6634](https://github.com/getsentry/sentry-java/pull/6634))
2930

3031
### Dependencies
3132

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -931,7 +931,8 @@ static boolean registerNetworkCallback(
931931
return false;
932932
}
933933
try {
934-
if (handler != null) {
934+
// The Handler overload is API 26+. Below that, fall back to the default callback thread.
935+
if (handler != null && buildInfoProvider.getSdkInfoVersion() >= Build.VERSION_CODES.O) {
935936
connectivityManager.registerDefaultNetworkCallback(networkCallback, handler);
936937
} else {
937938
connectivityManager.registerDefaultNetworkCallback(networkCallback);

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -987,6 +987,7 @@ class AndroidConnectionStatusProviderTest {
987987
@Test
988988
fun `registerNetworkCallback with a custom handlers calls connectivityManager with it`() {
989989
val customHandler = object : Handler(Looper.getMainLooper()) {}
990+
whenever(buildInfo.sdkInfoVersion).thenReturn(Build.VERSION_CODES.O)
990991
whenever(contextMock.getSystemService(any())).thenReturn(connectivityManager)
991992
AndroidConnectionStatusProvider.registerNetworkCallback(
992993
contextMock,
@@ -999,4 +1000,25 @@ class AndroidConnectionStatusProviderTest {
9991000
verify(connectivityManager)
10001001
.registerDefaultNetworkCallback(any<NetworkCallback>(), argThat { this == customHandler })
10011002
}
1003+
1004+
@Test
1005+
fun `registerNetworkCallback with a custom handler below API 26 registers without it`() {
1006+
val customHandler = object : Handler(Looper.getMainLooper()) {}
1007+
val callback = mock<NetworkCallback>()
1008+
whenever(buildInfo.sdkInfoVersion).thenReturn(Build.VERSION_CODES.N_MR1)
1009+
whenever(contextMock.getSystemService(any())).thenReturn(connectivityManager)
1010+
val registered =
1011+
AndroidConnectionStatusProvider.registerNetworkCallback(
1012+
contextMock,
1013+
logger,
1014+
buildInfo,
1015+
customHandler,
1016+
callback,
1017+
)
1018+
1019+
assertThat(registered).isTrue()
1020+
verify(connectivityManager).registerDefaultNetworkCallback(callback)
1021+
verify(connectivityManager, never())
1022+
.registerDefaultNetworkCallback(eq(callback), any<Handler>())
1023+
}
10021024
}

0 commit comments

Comments
 (0)