Skip to content

Commit 177bc5d

Browse files
committed
Add API check and more documentation
1 parent 07bca37 commit 177bc5d

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

firebase-perf/src/main/java/com/google/firebase/perf/metrics/AppStartTrace.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -324,13 +324,14 @@ private void recordOnDrawFrontOfQueue() {
324324
}
325325

326326
/**
327-
* Sets the `isStartedFromBackground` flag to `true` if the `mainThreadRunnable` time was set
328-
* from the `StartFromBackgroundRunnable` more than 100ms prior to the first time
329-
* `onActivityCreated` was called.
327+
* Sets the `isStartedFromBackground` flag to `true` if the `mainThreadRunnableTime` was set
328+
* from the `StartFromBackgroundRunnable`.
330329
* <p>
331-
* If it was called less than 100ms before `onActivityCreated` the assumption is that it was a
332-
* change in order on API 34+ devices where the runnable executes before the activity
333-
* lifecycle callbacks.
330+
* If it's prior to API 34, it's always set to true if `mainThreadRunnableTime` was set.
331+
* <p>
332+
* If it's on or after API 34, and it was called less than 100ms before `onActivityCreated`, the
333+
* assumption is that it was called immediately before the activity lifecycle callbacks in a
334+
* foreground start.
334335
* See https://github.com/firebase/firebase-android-sdk/issues/5920.
335336
*/
336337
private void resolveIsStartedFromBackground() {
@@ -340,12 +341,17 @@ private void resolveIsStartedFromBackground() {
340341
return;
341342
}
342343

343-
// Set it to true if the runnable ran more than 100ms prior to onActivityCreated()
344-
if (mainThreadRunnableTime.getDurationMicros() > MAX_BACKGROUND_RUNNABLE_DELAY) {
344+
// If the `minaThreadRunnableTime` was set prior to API 34, it's always assumed that's it's
345+
// a background start.
346+
// Otherwise it's assumed to be a background start if the runnable was set more than 100ms
347+
// before the first `onActivityCreated` call.
348+
// TODO(b/339891952): Investigate removing the API check, and setting a more precise delay.
349+
if ((Build.VERSION.SDK_INT < 34)
350+
|| (mainThreadRunnableTime.getDurationMicros() > MAX_BACKGROUND_RUNNABLE_DELAY)) {
345351
isStartedFromBackground = true;
346352
}
347353

348-
// Set this to null to prevent additional checks if `onActivityCreated()` is called again.
354+
// Set this to null to prevent additional checks.
349355
mainThreadRunnableTime = null;
350356
}
351357

0 commit comments

Comments
 (0)