Skip to content

Commit 48f9273

Browse files
authored
Merge pull request #5831 from getsentry/fix/data-collection-device-info-options
fix(android): [Data Collection 15] Scope device info cache to SDK options
2 parents d9d0356 + e30bf6b commit 48f9273

7 files changed

Lines changed: 58 additions & 28 deletions

File tree

‎sentry-android-core/api/sentry-android-core.api‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,6 @@ public final class io/sentry/android/core/DeviceInfoUtil {
283283
public fun getSplitApksInfo ()Lio/sentry/android/core/ContextUtils$SplitApksInfo;
284284
public fun getTotalMemory ()Ljava/lang/Long;
285285
public static fun isCharging (Landroid/content/Intent;Lio/sentry/SentryOptions;)Ljava/lang/Boolean;
286-
public static fun resetInstance ()V
287286
}
288287

289288
public abstract class io/sentry/android/core/EnvelopeFileObserverIntegration : io/sentry/Integration, java/io/Closeable {

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

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,13 @@
1515
import android.os.SystemClock;
1616
import android.util.DisplayMetrics;
1717
import io.sentry.DateUtils;
18-
import io.sentry.ISentryLifecycleToken;
1918
import io.sentry.SentryLevel;
2019
import io.sentry.SentryOptions;
2120
import io.sentry.android.core.internal.util.CpuInfoUtils;
2221
import io.sentry.android.core.internal.util.DeviceOrientations;
2322
import io.sentry.android.core.internal.util.RootChecker;
2423
import io.sentry.protocol.Device;
2524
import io.sentry.protocol.OperatingSystem;
26-
import io.sentry.util.AutoClosableReentrantLock;
2725
import java.io.File;
2826
import java.util.Calendar;
2927
import java.util.Collections;
@@ -34,17 +32,10 @@
3432
import org.jetbrains.annotations.ApiStatus;
3533
import org.jetbrains.annotations.NotNull;
3634
import org.jetbrains.annotations.Nullable;
37-
import org.jetbrains.annotations.TestOnly;
3835

3936
@ApiStatus.Internal
4037
public final class DeviceInfoUtil {
4138

42-
@SuppressLint("StaticFieldLeak")
43-
private static volatile DeviceInfoUtil instance;
44-
45-
private static final @NotNull AutoClosableReentrantLock staticLock =
46-
new AutoClosableReentrantLock();
47-
4839
private final @NotNull Context context;
4940
private final @NotNull SentryAndroidOptions options;
5041
private final @NotNull BuildInfoProvider buildInfoProvider;
@@ -80,19 +71,7 @@ public DeviceInfoUtil(
8071
@NotNull
8172
public static DeviceInfoUtil getInstance(
8273
final @NotNull Context context, final @NotNull SentryAndroidOptions options) {
83-
if (instance == null) {
84-
try (final @NotNull ISentryLifecycleToken ignored = staticLock.acquire()) {
85-
if (instance == null) {
86-
instance = new DeviceInfoUtil(ContextUtils.getApplicationContext(context), options);
87-
}
88-
}
89-
}
90-
return instance;
91-
}
92-
93-
@TestOnly
94-
public static void resetInstance() {
95-
instance = null;
74+
return options.getOrCreateDeviceInfoUtil(context);
9675
}
9776

9877
// we can get some inspiration here

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
import android.app.Activity;
44
import android.app.ActivityManager;
55
import android.app.ApplicationExitInfo;
6+
import android.content.Context;
67
import io.sentry.Hint;
78
import io.sentry.IScope;
9+
import io.sentry.ISentryLifecycleToken;
810
import io.sentry.ISpan;
911
import io.sentry.Sentry;
1012
import io.sentry.SentryEvent;
@@ -141,6 +143,8 @@ public final class SentryAndroidOptions extends SentryOptions {
141143
/** Enables or disables collecting of external storage context. */
142144
private boolean collectExternalStorageContext = false;
143145

146+
private volatile @Nullable DeviceInfoUtil deviceInfoUtil;
147+
144148
/**
145149
* Controls how many seconds to wait for sending events in case there were Startup Crashes in the
146150
* previous run. Sentry SDKs normally send events from a background queue, but in the case of
@@ -200,6 +204,18 @@ public final class SentryAndroidOptions extends SentryOptions {
200204
/** Enable or disable intent extras reporting for system event breadcrumbs. Default is false. */
201205
private boolean enableSystemEventBreadcrumbsExtras = false;
202206

207+
@NotNull
208+
DeviceInfoUtil getOrCreateDeviceInfoUtil(final @NotNull Context context) {
209+
if (deviceInfoUtil == null) {
210+
try (final @NotNull ISentryLifecycleToken ignored = lock.acquire()) {
211+
if (deviceInfoUtil == null) {
212+
deviceInfoUtil = new DeviceInfoUtil(ContextUtils.getApplicationContext(context), this);
213+
}
214+
}
215+
}
216+
return deviceInfoUtil;
217+
}
218+
203219
public interface BeforeCaptureCallback {
204220

205221
/**

‎sentry-android-core/src/test/java/io/sentry/android/core/ApplicationExitInfoEventProcessorTest.kt‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,6 @@ class ApplicationExitInfoEventProcessorTest {
201201

202202
@BeforeTest
203203
fun `set up`() {
204-
DeviceInfoUtil.resetInstance()
205-
ContextUtils.resetInstance()
206204
fixture.context = ApplicationProvider.getApplicationContext()
207205
}
208206

‎sentry-android-core/src/test/java/io/sentry/android/core/DefaultAndroidEventProcessorTest.kt‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ class DefaultAndroidEventProcessorTest {
8787
fun `set up`() {
8888
context = ApplicationProvider.getApplicationContext()
8989
AppState.getInstance().resetInstance()
90-
DeviceInfoUtil.resetInstance()
9190
CpuInfoUtils.getInstance().clear()
9291
}
9392

‎sentry-android-core/src/test/java/io/sentry/android/core/DeviceInfoUtilTest.kt‎

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ import kotlin.test.BeforeTest
1515
import kotlin.test.Test
1616
import kotlin.test.assertEquals
1717
import kotlin.test.assertNotNull
18+
import kotlin.test.assertNotSame
1819
import kotlin.test.assertNull
20+
import kotlin.test.assertSame
1921
import org.junit.runner.RunWith
2022
import org.robolectric.annotation.Config
2123

@@ -32,7 +34,45 @@ class DeviceInfoUtilTest {
3234
.putExtra(BatteryManager.EXTRA_LEVEL, 75)
3335
.putExtra(BatteryManager.EXTRA_PLUGGED, 0)
3436
)
35-
DeviceInfoUtil.resetInstance()
37+
}
38+
39+
@Test
40+
fun `same options reuse device info util`() {
41+
val options = SentryAndroidOptions()
42+
43+
val first = DeviceInfoUtil.getInstance(context, options)
44+
val second = DeviceInfoUtil.getInstance(context, options)
45+
46+
assertSame(first, second)
47+
}
48+
49+
@Test
50+
fun `different options use isolated device info utils`() {
51+
val enabledOptions =
52+
SentryAndroidOptions().apply {
53+
dataCollection.setUserInfo(true)
54+
isCollectAdditionalContext = true
55+
isEnableRootCheck = true
56+
}
57+
val disabledOptions =
58+
SentryAndroidOptions().apply {
59+
dataCollection.setUserInfo(false)
60+
isCollectAdditionalContext = false
61+
isEnableRootCheck = false
62+
}
63+
64+
val enabled = DeviceInfoUtil.getInstance(context, enabledOptions)
65+
val disabled = DeviceInfoUtil.getInstance(context, disabledOptions)
66+
val enabledDevice = enabled.collectDeviceInformation(true, false)
67+
val disabledDevice = disabled.collectDeviceInformation(true, false)
68+
69+
assertNotSame(enabled, disabled)
70+
assertNotNull(enabledDevice.id)
71+
assertNotNull(enabledDevice.storageSize)
72+
assertNotNull(enabled.operatingSystem.isRooted)
73+
assertNull(disabledDevice.id)
74+
assertNull(disabledDevice.storageSize)
75+
assertNull(disabled.operatingSystem.isRooted)
3676
}
3777

3878
@Test

‎sentry-android-core/src/test/java/io/sentry/android/core/InternalSentrySdkTest.kt‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,6 @@ class InternalSentrySdkTest {
259259
fun `set up`() {
260260
Sentry.close()
261261
context = ApplicationProvider.getApplicationContext()
262-
DeviceInfoUtil.resetInstance()
263262
}
264263

265264
@Test

0 commit comments

Comments
 (0)