3333import io .sentry .SentryEvent ;
3434import io .sentry .SentryExceptionFactory ;
3535import io .sentry .SentryLevel ;
36+ import io .sentry .SentryOptions ;
3637import io .sentry .SentryStackTraceFactory ;
3738import io .sentry .SpanContext ;
3839import io .sentry .android .core .internal .util .CpuInfoUtils ;
@@ -83,13 +84,16 @@ public final class AnrV2EventProcessor implements BackfillingEventProcessor {
8384
8485 private final @ NotNull SentryExceptionFactory sentryExceptionFactory ;
8586
87+ private final @ Nullable PersistingScopeObserver persistingScopeObserver ;
88+
8689 public AnrV2EventProcessor (
8790 final @ NotNull Context context ,
8891 final @ NotNull SentryAndroidOptions options ,
8992 final @ NotNull BuildInfoProvider buildInfoProvider ) {
9093 this .context = ContextUtils .getApplicationContext (context );
9194 this .options = options ;
9295 this .buildInfoProvider = buildInfoProvider ;
96+ this .persistingScopeObserver = options .findPersistingScopeObserver ();
9397
9498 final SentryStackTraceFactory sentryStackTraceFactory =
9599 new SentryStackTraceFactory (this .options );
@@ -188,8 +192,7 @@ private boolean sampleReplay(final @NotNull SentryEvent event) {
188192 }
189193
190194 private void setReplayId (final @ NotNull SentryEvent event ) {
191- @ Nullable
192- String persistedReplayId = PersistingScopeObserver .read (options , REPLAY_FILENAME , String .class );
195+ @ Nullable String persistedReplayId = readFromDisk (options , REPLAY_FILENAME , String .class );
193196 final @ NotNull File replayFolder =
194197 new File (options .getCacheDirPath (), "replay_" + persistedReplayId );
195198 if (!replayFolder .exists ()) {
@@ -224,8 +227,7 @@ private void setReplayId(final @NotNull SentryEvent event) {
224227 }
225228
226229 private void setTrace (final @ NotNull SentryEvent event ) {
227- final SpanContext spanContext =
228- PersistingScopeObserver .read (options , TRACE_FILENAME , SpanContext .class );
230+ final SpanContext spanContext = readFromDisk (options , TRACE_FILENAME , SpanContext .class );
229231 if (event .getContexts ().getTrace () == null ) {
230232 if (spanContext != null
231233 && spanContext .getSpanId () != null
@@ -236,8 +238,7 @@ private void setTrace(final @NotNull SentryEvent event) {
236238 }
237239
238240 private void setLevel (final @ NotNull SentryEvent event ) {
239- final SentryLevel level =
240- PersistingScopeObserver .read (options , LEVEL_FILENAME , SentryLevel .class );
241+ final SentryLevel level = readFromDisk (options , LEVEL_FILENAME , SentryLevel .class );
241242 if (event .getLevel () == null ) {
242243 event .setLevel (level );
243244 }
@@ -246,7 +247,7 @@ private void setLevel(final @NotNull SentryEvent event) {
246247 @ SuppressWarnings ("unchecked" )
247248 private void setFingerprints (final @ NotNull SentryEvent event , final @ NotNull Object hint ) {
248249 final List <String > fingerprint =
249- (List <String >) PersistingScopeObserver . read (options , FINGERPRINT_FILENAME , List .class );
250+ (List <String >) readFromDisk (options , FINGERPRINT_FILENAME , List .class );
250251 if (event .getFingerprints () == null ) {
251252 event .setFingerprints (fingerprint );
252253 }
@@ -262,16 +263,14 @@ private void setFingerprints(final @NotNull SentryEvent event, final @NotNull Ob
262263 }
263264
264265 private void setTransaction (final @ NotNull SentryEvent event ) {
265- final String transaction =
266- PersistingScopeObserver .read (options , TRANSACTION_FILENAME , String .class );
266+ final String transaction = readFromDisk (options , TRANSACTION_FILENAME , String .class );
267267 if (event .getTransaction () == null ) {
268268 event .setTransaction (transaction );
269269 }
270270 }
271271
272272 private void setContexts (final @ NotNull SentryBaseEvent event ) {
273- final Contexts persistedContexts =
274- PersistingScopeObserver .read (options , CONTEXTS_FILENAME , Contexts .class );
273+ final Contexts persistedContexts = readFromDisk (options , CONTEXTS_FILENAME , Contexts .class );
275274 if (persistedContexts == null ) {
276275 return ;
277276 }
@@ -291,7 +290,7 @@ private void setContexts(final @NotNull SentryBaseEvent event) {
291290 @ SuppressWarnings ("unchecked" )
292291 private void setExtras (final @ NotNull SentryBaseEvent event ) {
293292 final Map <String , Object > extras =
294- (Map <String , Object >) PersistingScopeObserver . read (options , EXTRAS_FILENAME , Map .class );
293+ (Map <String , Object >) readFromDisk (options , EXTRAS_FILENAME , Map .class );
295294 if (extras == null ) {
296295 return ;
297296 }
@@ -309,14 +308,12 @@ private void setExtras(final @NotNull SentryBaseEvent event) {
309308 @ SuppressWarnings ("unchecked" )
310309 private void setBreadcrumbs (final @ NotNull SentryBaseEvent event ) {
311310 final List <Breadcrumb > breadcrumbs =
312- (List <Breadcrumb >)
313- PersistingScopeObserver .read (
314- options , BREADCRUMBS_FILENAME , List .class , new Breadcrumb .Deserializer ());
311+ (List <Breadcrumb >) readFromDisk (options , BREADCRUMBS_FILENAME , List .class );
315312 if (breadcrumbs == null ) {
316313 return ;
317314 }
318315 if (event .getBreadcrumbs () == null ) {
319- event .setBreadcrumbs (new ArrayList <>( breadcrumbs ) );
316+ event .setBreadcrumbs (breadcrumbs );
320317 } else {
321318 event .getBreadcrumbs ().addAll (breadcrumbs );
322319 }
@@ -326,7 +323,7 @@ private void setBreadcrumbs(final @NotNull SentryBaseEvent event) {
326323 private void setScopeTags (final @ NotNull SentryBaseEvent event ) {
327324 final Map <String , String > tags =
328325 (Map <String , String >)
329- PersistingScopeObserver . read (options , PersistingScopeObserver .TAGS_FILENAME , Map .class );
326+ readFromDisk (options , PersistingScopeObserver .TAGS_FILENAME , Map .class );
330327 if (tags == null ) {
331328 return ;
332329 }
@@ -343,19 +340,29 @@ private void setScopeTags(final @NotNull SentryBaseEvent event) {
343340
344341 private void setUser (final @ NotNull SentryBaseEvent event ) {
345342 if (event .getUser () == null ) {
346- final User user = PersistingScopeObserver . read (options , USER_FILENAME , User .class );
343+ final User user = readFromDisk (options , USER_FILENAME , User .class );
347344 event .setUser (user );
348345 }
349346 }
350347
351348 private void setRequest (final @ NotNull SentryBaseEvent event ) {
352349 if (event .getRequest () == null ) {
353- final Request request =
354- PersistingScopeObserver .read (options , REQUEST_FILENAME , Request .class );
350+ final Request request = readFromDisk (options , REQUEST_FILENAME , Request .class );
355351 event .setRequest (request );
356352 }
357353 }
358354
355+ private <T > @ Nullable T readFromDisk (
356+ final @ NotNull SentryOptions options ,
357+ final @ NotNull String fileName ,
358+ final @ NotNull Class <T > clazz ) {
359+ if (persistingScopeObserver == null ) {
360+ return null ;
361+ }
362+
363+ return persistingScopeObserver .read (options , fileName , clazz );
364+ }
365+
359366 // endregion
360367
361368 // region options persisted values
0 commit comments