Skip to content

Commit c56405c

Browse files
committed
fix(android): Read the connection type and technology together
Reading the connection type and the network technology separately let a connectivity change land between them, so an event could report a wifi connection with a cellular technology. Both now come from one accessor that derives them from a single cache read.
1 parent 4090d4e commit c56405c

3 files changed

Lines changed: 38 additions & 18 deletions

File tree

‎sentry-android-core/src/main/java/io/sentry/android/core/DeviceInfoUtil.java‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -230,18 +230,18 @@ private void setDeviceIO(
230230
}
231231

232232
if (device.getConnectionType() == null) {
233-
// wifi, ethernet or cellular, null if none
234-
device.setConnectionType(options.getConnectionStatusProvider().getConnectionType());
235-
}
236-
237-
if (device.getConnectionEffectiveType() == null) {
238233
final @NotNull IConnectionStatusProvider connectionStatusProvider =
239234
options.getConnectionStatusProvider();
240235
if (connectionStatusProvider instanceof AndroidConnectionStatusProvider) {
236+
// Reading both at once keeps the type and the technology describing the same network.
237+
final AndroidConnectionStatusProvider.Connection connection =
238+
((AndroidConnectionStatusProvider) connectionStatusProvider).getConnection();
239+
// wifi, ethernet or cellular, null if none
240+
device.setConnectionType(connection.type);
241241
// 2g, 3g, 4g or 5g, null unless the device is on a known cellular technology
242-
device.setConnectionEffectiveType(
243-
((AndroidConnectionStatusProvider) connectionStatusProvider)
244-
.getConnectionEffectiveType());
242+
device.setConnectionEffectiveType(connection.effectiveType);
243+
} else {
244+
device.setConnectionType(connectionStatusProvider.getConnectionType());
245245
}
246246
}
247247
}

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

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -178,18 +178,38 @@ private boolean isNetworkEffectivelyConnected(
178178
}
179179

180180
/**
181-
* The generation of the cellular network technology currently used for data, for example {@code
182-
* 5g}, or {@code null} when the connection is not cellular or the technology is unknown. Maps to
183-
* the {@code network.connection.effective_type} attribute.
181+
* The connection type and, for cellular connections, the generation of the network technology.
182+
*
183+
* <p>Both are derived from one cache read, so they always describe the same network instead of
184+
* straddling a connectivity change.
184185
*/
185-
public @Nullable String getConnectionEffectiveType() {
186+
public @NotNull Connection getConnection() {
186187
if (!isCacheValid()) {
187188
updateCache(null);
188189
}
189-
if (!"cellular".equals(getConnectionTypeFromCache())) {
190-
return null;
190+
final @Nullable String connectionType = getConnectionTypeFromCache();
191+
if (!"cellular".equals(connectionType)) {
192+
return new Connection(connectionType, null);
193+
}
194+
return new Connection(
195+
connectionType, cellularNetworkTechnologyProvider.getCellularNetworkTechnology());
196+
}
197+
198+
/** The connection type and the generation of the cellular network technology, if any. */
199+
public static final class Connection {
200+
/** Maps to the {@code network.connection.type} attribute, {@code null} when unknown. */
201+
public final @Nullable String type;
202+
203+
/**
204+
* Maps to the {@code network.connection.effective_type} attribute, for example {@code 5g}.
205+
* {@code null} when the connection is not cellular or the technology is unknown.
206+
*/
207+
public final @Nullable String effectiveType;
208+
209+
Connection(final @Nullable String type, final @Nullable String effectiveType) {
210+
this.type = type;
211+
this.effectiveType = effectiveType;
191212
}
192-
return cellularNetworkTechnologyProvider.getCellularNetworkTechnology();
193213
}
194214

195215
private void ensureNetworkCallbackRegistered() {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ class AndroidConnectionStatusProviderTest {
288288

289289
// The connection type keeps its documented values, the generation is reported separately.
290290
assertEquals("cellular", connectionStatusProvider.connectionType)
291-
assertEquals("5g", connectionStatusProvider.connectionEffectiveType)
291+
assertEquals("5g", connectionStatusProvider.connection.effectiveType)
292292
}
293293

294294
@Test
@@ -307,7 +307,7 @@ class AndroidConnectionStatusProviderTest {
307307
.thenReturn(PERMISSION_DENIED)
308308

309309
assertEquals("cellular", connectionStatusProvider.connectionType)
310-
assertNull(connectionStatusProvider.connectionEffectiveType)
310+
assertNull(connectionStatusProvider.connection.effectiveType)
311311
verify(telephonyManager, never()).dataNetworkType
312312
}
313313

@@ -322,7 +322,7 @@ class AndroidConnectionStatusProviderTest {
322322
whenever(telephonyManager.dataNetworkType).thenReturn(TelephonyManager.NETWORK_TYPE_NR)
323323

324324
assertEquals("wifi", connectionStatusProvider.connectionType)
325-
assertNull(connectionStatusProvider.connectionEffectiveType)
325+
assertNull(connectionStatusProvider.connection.effectiveType)
326326
}
327327

328328
@Test

0 commit comments

Comments
 (0)