@@ -75,8 +75,6 @@ public class AppStartTrace implements ActivityLifecycleCallbacks, LifecycleObser
75
75
private static final @ NonNull Timer PERF_CLASS_LOAD_TIME = new Clock ().getTime ();
76
76
private static final long MAX_LATENCY_BEFORE_UI_INIT = TimeUnit .MINUTES .toMicros (1 );
77
77
78
- private static final long MAX_BACKGROUND_RUNNABLE_DELAY = TimeUnit .MILLISECONDS .toMicros (100 );
79
-
80
78
// Core pool size 0 allows threads to shut down if they're idle
81
79
private static final int CORE_POOL_SIZE = 0 ;
82
80
private static final int MAX_POOL_SIZE = 1 ; // Only need single thread
@@ -113,8 +111,6 @@ public class AppStartTrace implements ActivityLifecycleCallbacks, LifecycleObser
113
111
private final @ Nullable Timer processStartTime ;
114
112
private final @ Nullable Timer firebaseClassLoadTime ;
115
113
private Timer onCreateTime = null ;
116
-
117
- private Timer mainThreadRunnableTime = null ;
118
114
private Timer onStartTime = null ;
119
115
private Timer onResumeTime = null ;
120
116
private Timer firstForegroundTime = null ;
@@ -323,26 +319,8 @@ private void recordOnDrawFrontOfQueue() {
323
319
logExperimentTrace (this .experimentTtid );
324
320
}
325
321
326
- private void resolveIsStartedFromBackground () {
327
- // If the mainThreadRunnableTime is null, either the runnable hasn't run, or this check has
328
- // already been made.
329
- if (mainThreadRunnableTime == null ) {
330
- return ;
331
- }
332
-
333
- // Set it to true if the runnable ran more than 100ms prior to onActivityCreated()
334
- if (mainThreadRunnableTime .getDurationMicros () > MAX_BACKGROUND_RUNNABLE_DELAY ) {
335
- isStartedFromBackground = true ;
336
- }
337
-
338
- // Set this to null to prevent additional checks if `onActivityCreated()` is called again.
339
- mainThreadRunnableTime = null ;
340
- }
341
-
342
322
@ Override
343
323
public synchronized void onActivityCreated (Activity activity , Bundle savedInstanceState ) {
344
- resolveIsStartedFromBackground ();
345
-
346
324
if (isStartedFromBackground || onCreateTime != null // An activity already called onCreate()
347
325
) {
348
326
return ;
@@ -582,7 +560,8 @@ public static boolean isScreenOn(Context appContext) {
582
560
* We use StartFromBackgroundRunnable to detect if app is started from background or foreground.
583
561
* If app is started from background, we do not generate AppStart trace. This runnable is posted
584
562
* to main UI thread from FirebasePerfEarly. If app is started from background, this runnable will
585
- * be executed earlier than 100ms of any activity's onCreate() method.
563
+ * be executed before any activity's onCreate() method. If app is started from foreground,
564
+ * activity's onCreate() method is executed before this runnable.
586
565
*/
587
566
public static class StartFromBackgroundRunnable implements Runnable {
588
567
private final AppStartTrace trace ;
@@ -593,7 +572,10 @@ public StartFromBackgroundRunnable(final AppStartTrace trace) {
593
572
594
573
@ Override
595
574
public void run () {
596
- trace .mainThreadRunnableTime = new Timer ();
575
+ // if no activity has ever been created.
576
+ if (trace .onCreateTime == null ) {
577
+ trace .isStartedFromBackground = true ;
578
+ }
597
579
}
598
580
}
599
581
@@ -632,7 +614,7 @@ Timer getOnResumeTime() {
632
614
}
633
615
634
616
@ VisibleForTesting
635
- void setMainThreadRunnableTime ( Timer timer ) {
636
- mainThreadRunnableTime = timer ;
617
+ void setIsStartFromBackground ( ) {
618
+ isStartedFromBackground = true ;
637
619
}
638
620
}
0 commit comments