28
28
import io .sentry .TransactionOptions ;
29
29
import io .sentry .android .core .internal .util .ClassUtil ;
30
30
import io .sentry .android .core .internal .util .FirstDrawDoneListener ;
31
- import io .sentry .android .core .performance .ActivityLifecycleTimeSpan ;
31
+ import io .sentry .android .core .performance .ActivityLifecycleSpanHelper ;
32
32
import io .sentry .android .core .performance .AppStartMetrics ;
33
33
import io .sentry .android .core .performance .TimeSpan ;
34
34
import io .sentry .protocol .MeasurementValue ;
@@ -77,7 +77,7 @@ public final class ActivityLifecycleIntegration
77
77
private @ Nullable ISpan appStartSpan ;
78
78
private final @ NotNull WeakHashMap <Activity , ISpan > ttidSpanMap = new WeakHashMap <>();
79
79
private final @ NotNull WeakHashMap <Activity , ISpan > ttfdSpanMap = new WeakHashMap <>();
80
- private final @ NotNull WeakHashMap <Activity , ActivityLifecycleTimeSpan > activityLifecycleMap =
80
+ private final @ NotNull WeakHashMap <Activity , ActivityLifecycleSpanHelper > activitySpanHelpers =
81
81
new WeakHashMap <>();
82
82
private @ NotNull SentryDate lastPausedTime = new SentryNanotimeDate (new Date (0 ), 0 );
83
83
private long lastPausedUptimeMillis = 0 ;
@@ -374,6 +374,9 @@ private void finishTransaction(
374
374
@ Override
375
375
public void onActivityPreCreated (
376
376
final @ NotNull Activity activity , final @ Nullable Bundle savedInstanceState ) {
377
+ final ActivityLifecycleSpanHelper helper =
378
+ new ActivityLifecycleSpanHelper (activity .getClass ().getName ());
379
+ activitySpanHelpers .put (activity , helper );
377
380
// The very first activity start timestamp cannot be set to the class instantiation time, as it
378
381
// may happen before an activity is started (service, broadcast receiver, etc). So we set it
379
382
// here.
@@ -385,10 +388,7 @@ public void onActivityPreCreated(
385
388
? hub .getOptions ().getDateProvider ().now ()
386
389
: AndroidDateUtils .getCurrentSentryDateTime ();
387
390
lastPausedUptimeMillis = SystemClock .uptimeMillis ();
388
-
389
- final @ NotNull ActivityLifecycleTimeSpan timeSpan = new ActivityLifecycleTimeSpan ();
390
- timeSpan .getOnCreate ().setStartedAt (lastPausedUptimeMillis );
391
- activityLifecycleMap .put (activity , timeSpan );
391
+ helper .setOnCreateStartTimestamp (lastPausedTime );
392
392
}
393
393
394
394
@ Override
@@ -415,26 +415,20 @@ public synchronized void onActivityCreated(
415
415
@ Override
416
416
public void onActivityPostCreated (
417
417
final @ NotNull Activity activity , final @ Nullable Bundle savedInstanceState ) {
418
- if (appStartSpan == null ) {
419
- activityLifecycleMap .remove (activity );
420
- return ;
421
- }
422
-
423
- final @ Nullable ActivityLifecycleTimeSpan timeSpan = activityLifecycleMap .get (activity );
424
- if (timeSpan != null ) {
425
- timeSpan .getOnCreate ().stop ();
426
- timeSpan .getOnCreate ().setDescription (activity .getClass ().getName () + ".onCreate" );
418
+ final ActivityLifecycleSpanHelper helper = activitySpanHelpers .get (activity );
419
+ if (helper != null ) {
420
+ helper .createAndStopOnCreateSpan (appStartSpan );
427
421
}
428
422
}
429
423
430
424
@ Override
431
425
public void onActivityPreStarted (final @ NotNull Activity activity ) {
432
- if ( appStartSpan == null ) {
433
- return ;
434
- }
435
- final @ Nullable ActivityLifecycleTimeSpan timeSpan = activityLifecycleMap . get ( activity );
436
- if ( timeSpan != null ) {
437
- timeSpan . getOnStart (). setStartedAt ( SystemClock . uptimeMillis ());
426
+ final ActivityLifecycleSpanHelper helper = activitySpanHelpers . get ( activity );
427
+ if ( helper != null ) {
428
+ helper . setOnStartStartTimestamp (
429
+ options != null
430
+ ? options . getDateProvider (). now ()
431
+ : AndroidDateUtils . getCurrentSentryDateTime ());
438
432
}
439
433
}
440
434
@@ -457,14 +451,11 @@ public synchronized void onActivityStarted(final @NotNull Activity activity) {
457
451
458
452
@ Override
459
453
public void onActivityPostStarted (final @ NotNull Activity activity ) {
460
- final @ Nullable ActivityLifecycleTimeSpan timeSpan = activityLifecycleMap .remove (activity );
461
- if (appStartSpan == null ) {
462
- return ;
463
- }
464
- if (timeSpan != null ) {
465
- timeSpan .getOnStart ().stop ();
466
- timeSpan .getOnStart ().setDescription (activity .getClass ().getName () + ".onStart" );
467
- AppStartMetrics .getInstance ().addActivityLifecycleTimeSpans (timeSpan );
454
+ final ActivityLifecycleSpanHelper helper = activitySpanHelpers .get (activity );
455
+ if (helper != null ) {
456
+ helper .createAndStopOnStartSpan (appStartSpan );
457
+ // Needed to handle hybrid SDKs
458
+ helper .saveSpanToAppStartMetrics ();
468
459
}
469
460
}
470
461
@@ -523,7 +514,10 @@ public void onActivitySaveInstanceState(
523
514
524
515
@ Override
525
516
public synchronized void onActivityDestroyed (final @ NotNull Activity activity ) {
526
- activityLifecycleMap .remove (activity );
517
+ final ActivityLifecycleSpanHelper helper = activitySpanHelpers .remove (activity );
518
+ if (helper != null ) {
519
+ helper .clear ();
520
+ }
527
521
if (performanceEnabled ) {
528
522
529
523
// in case the appStartSpan isn't completed yet, we finish it as cancelled to avoid
@@ -563,7 +557,7 @@ private void clear() {
563
557
firstActivityCreated = false ;
564
558
lastPausedTime = new SentryNanotimeDate (new Date (0 ), 0 );
565
559
lastPausedUptimeMillis = 0 ;
566
- activityLifecycleMap .clear ();
560
+ activitySpanHelpers .clear ();
567
561
}
568
562
569
563
private void finishSpan (final @ Nullable ISpan span ) {
@@ -608,8 +602,7 @@ private void onFirstFrameDrawn(final @Nullable ISpan ttfdSpan, final @Nullable I
608
602
final @ NotNull TimeSpan appStartTimeSpan = appStartMetrics .getAppStartTimeSpan ();
609
603
final @ NotNull TimeSpan sdkInitTimeSpan = appStartMetrics .getSdkInitTimeSpan ();
610
604
611
- // in case the SentryPerformanceProvider is disabled it does not set the app start end times,
612
- // and we need to set the end time manually here
605
+ // and we need to set the end time of the app start here, after the first frame is drawn.
613
606
if (appStartTimeSpan .hasStarted () && appStartTimeSpan .hasNotStopped ()) {
614
607
appStartTimeSpan .stop ();
615
608
}
@@ -672,8 +665,8 @@ WeakHashMap<Activity, ITransaction> getActivitiesWithOngoingTransactions() {
672
665
673
666
@ TestOnly
674
667
@ NotNull
675
- WeakHashMap <Activity , ActivityLifecycleTimeSpan > getActivityLifecycleMap () {
676
- return activityLifecycleMap ;
668
+ WeakHashMap <Activity , ActivityLifecycleSpanHelper > getActivitySpanHelpers () {
669
+ return activitySpanHelpers ;
677
670
}
678
671
679
672
@ TestOnly
0 commit comments